Looping messages on the IBM MQ server

I have several hundred posts on my MQ server (using MQ.NET).

I am trying to read them one by one, but I have a problem with this. I don't have a 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 through all the messages in the queue, and if there is no message, I want to exit from it. Thanks in advance.

+3


source to share


2 answers


This is the line I was looking for by 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;

      

+1


source


See an example of programs installed with WMQ code. By default they will live in C:\Program Files (x86)\IBM\WebSphere MQ 7.5\tools\dotnet\samples\cs\base\

, and the one that I think you need is SimpleGet.cs

.

The problem you are having is you are reopening the queue. This resets the line pointer to the head of the eac queue time. The sample program shows how to open a queue once, then skip messages until a certain number is reached, 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;
                    }
                }
            }

      

IBM installation media with sample code can be downloaded as the SupportPac MC75 . If for some reason you need a back-end customer, they are available on the Supportpacs main page. However, keep in mind that there was a lot of engineering development in later versions and you are much better off with the latest version. Any version of the MQ client can work with any version of QMgr, but obviously the functionality you get is either only available on the client side (like the client.ini file) or on the server side, whatever this gives you QMgr level. In other words, using the V7.5 client with QMgr v7.0 works great, but it doesn't give you CHLAUTH rules because it doesn't have QMgr v7.0.

+3


source







All Articles