How do I browse a Websphere MQ Queue through all messages?
c#, ibm-mq
Solution
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.
Problem
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 } ```