Sharpeye500
Sharpeye500

Reputation: 9073

Looping over the messages in IBM MQ server

I have few hundreds of messages in my MQ server(using MQ .NET).

I am trying to read those one by one, however i am having issue in looping thro' that. I don't have any count/length property that i can use in this regard.

mqQueue - MQQueue mqQMgr - MQ QueueManager

   mqQueue = mqQMgr.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_BROWSE);
   mqQueue.Get(mqMsg, mqGetMsgOpts); 
   string readMessage = mqMsg.ReadString(mqMsg.MessageLength);

How can i loop thro' all the messages in the queue and if there is no message i want exit out. Thanks in advance.

Upvotes: 4

Views: 7541

Answers (2)

Sharpeye500
Sharpeye500

Reputation: 9073

This is the line i was looking for, moving the cursor to the next message, so that i can read the next message.

mGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT;

MQGetMessageOptions:

queue.Get(message);
Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
mGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT;

Upvotes: 1

T.Rob
T.Rob

Reputation: 31832

Have a look at the sample programs that installed with the WMQ code. By default, these will live in C:\Program Files (x86)\IBM\WebSphere MQ 7.5\tools\dotnet\samples\cs\base\ and the one I think you want is SimpleGet.cs.

The problem you are having is that you are opening the queue repeatedly. That resets the rowse pointer to the head of the queue eac time. The sample program shows how to open the queue once, then loop through messages until reaching a certain number or until the queue is empty, whichever comes first.

            // create connection
            Console.Write("Connecting to queue manager.. ");
            queueManager = new MQQueueManager(queueManagerName, properties);
            Console.WriteLine("done");

            // accessing queue
            Console.Write("Accessing queue " + queueName + ".. ");
            queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
            Console.WriteLine("done");

            // getting messages continuously
            for (int i = 1; i <= numberOfMsgs; i++)
            {
                // creating a message object
                message = new MQMessage();

                try
                {
                    queue.Get(message);
                    Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
                    message.ClearMessage();
                }
                catch (MQException mqe)
                {
                    if (mqe.ReasonCode == 2033)
                    {
                        Console.WriteLine("No message available");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                        break;
                    }
                }
            }

The IBM install media with the code samples is downloadable as SupportPac MC75. If for some reason you need a back-level client, they are available from the SupportPacs main page. However, keep in mind that there has been a lot of engineering in the later releases and you are MUCH better off with the latest version. Any version of MQ client can work with any version of QMgr, but obviously the functionality you get is either that only on the client side (such as the client.ini file) or on the server side, whatever that level of QMgr gives you. In other words, using a V7.5 client with a v7.0 QMgr works fine, but it doesn't give you CHLAUTH rules because the v7.0 QMgr doesn't have those.

Upvotes: 4

Related Questions