I've run into a frustrating problem browsing a queue all the way through its depth. I understood that the queue needs to be opened with the MQOO_BROWSE option among the open options. Then on the first read do a GET using the Get Messsage Option MQGMO_BROWSE_FIRST. Finally, the subsequent GET's should use the MQGMO_BROWSE_NEXT option.
The problem is, my attempts worked only to retrieve the first message! Upon the second GET, even with MQGMO_BROWSE_NEXT, the method threw MQRC_NO_MSG_AVAILABLE, even though there were 5 messages in the queue!
Here was the code I was using:
IList<string> Messages = new List<string>();
_queueManager = new MQQueueManager(QueueManagerName);
int openOptions = MQC.MQOO_BROWSE // open queue for browsing
_queue = QManager.AccessQueue(QueueName, openOptions);
MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST;
MQMessage msg = new MQMessage();
_queue.Get(msg, mqGetMsgOpts);
MQGetMessageOptions mqGetNextMsgOpts = new MQGetMessageOptions();
mqGetNextMsgOpts.Options = MQC.MQGMO_BROWSE_NEXT;
try
{
while (true)
{
string messageText = msg.ReadString(msg.MessageLength);
Messages.Add(messageText);
_queue.Get(msg, mqGetNextMsgOpts);
}
}
catch (MQException ex)
{
// Handle it
}
This was frustrating, but I was able to rely upon questions and answers posted here on SO to verify that I was on the right path. Unfortunately, none of the answers addressed subsequent GETs with browse, and I was stumped. The answer came to me after trying a number of fruitless other paths, and it was very simple when I finally came to it. I decided to post the Q&A of my solution that worked.
Apparently, once having done a GET into an MQMessage, unless you re-initialize it, the GET can't get the next message, and it throws the MQRC_NO_MSG_AVAILABLE exception immediately. Re-initializing the MQMessage instance before performing the GET solves the problem. I modified the code above by adding the needed line of code just before the GET in the while loop:
while (true)
{
string messageText = msg.ReadString(msg.MessageLength);
Messages.Add(messageText);
msg = new MQMessage();
_queue.Get(msg, mqGetNextMsgOpts);
}
Once I made this change, the routine browsed all the messages on the queue.
Yes, that's correct. A new instance of MQMessage
is required for every Get
method call. When the Get
method returns with a message, MQMessage
object gets initialized with the message headers and body of the message retrieved from a queue/topic. For example the MessageId
property gets initialized with message id of the message.
If the same object is used again to get another messages, the Get
actually attempts to get a message that has MessageID
of the message retrieved before. Since no message in the queue matches the given MessageID
, the Get
call returns with 2033
- MQRC_NO_MSG_AVAILABLE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With