IrmakA
IrmakA

Reputation: 45

Why do I get MQRC_NO_MSG_AVAILABLE?

I have the following code :

        MQQueueManager mqm = null;
        MQQueue mqQueue = null;
        try
        {
            mqm = new MQQueueManager("SWIFTQM", "SWIFTCHANNEL", "localhost");
            mqQueue = mqm.AccessQueue("SWIFTQ", MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);
            MQMessage mqMsg = new MQMessage();
            MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
            mqGetMsgOpts.Options |= MQC.MQGMO_SYNCPOINT;
            mqQueue.Get(mqMsg, mqGetMsgOpts);
            string s1 = "";
            if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
                s1 = mqMsg.ReadString(mqMsg.MessageLength);
            string s2 = "";
            mqQueue.Get(mqMsg, mqGetMsgOpts);
            if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0)
                s2 = mqMsg.ReadString(mqMsg.MessageLength);
        }
        finally
        {
            if (mqQueue != null)
                mqQueue.Close();
            if (mqm != null)
                mqm.Disconnect();
            if (mqm != null)
                mqm.Close();
        }

Though the queue has many messages, the second mqQueue.Get gets the "MQRC_NO_MSG_AVAILABLE" exception. Is there an option I am missing?

Upvotes: 2

Views: 7577

Answers (1)

Shashi
Shashi

Reputation: 15263

Yes, you have to do a mqMsg = new MQMessage(); before making the second Get call. The reason for this is MQMessage object gets initialized with the incoming message (headers and message body) during the Get call. For example the MessageId of the incoming message will be set to MQMessage.MessageId property.

As you can notice the second Get is being called with MQMessage object initialized in the first Get call. What this effectively means is that the second Get call is looking for a message with a message id that you have already received in the first get call. But that message had already been received in the first get call itself. Hence the second call fails with MQRC_NO_MSG_AVAILABLE reason code.

Also you are using MQGMO_SYNCPOINT option. But there is no commit being called in your code. This option is for receiving messages in a local transaction. If you don't intend to receive message under transaction, then you have to remove this option else do a mqm.Commit. If a Commit is not called messages will be redelivered when a connection is made again.

Upvotes: 5

Related Questions