Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I browse a Websphere MQ Queue through all messages?

Tags:

c#

ibm-mq

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
}
like image 824
Cyberherbalist Avatar asked Nov 30 '22 01:11

Cyberherbalist


2 Answers

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.

like image 167
Cyberherbalist Avatar answered Dec 15 '22 23:12

Cyberherbalist


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.

like image 26
Shashi Avatar answered Dec 15 '22 23:12

Shashi