C# and IBM WebSphere MQ message borwsing
-
Hello, Can anyone help me, how can i browse the all messages in the queue? i have found several examples to get or put the messages, but about the browsing i haven't. The folloing example is browse the first messages, but i would like to see the all messages...
MQQueueManager mqQMgr; MQQueue mqQueue; MQMessage mqMsg = new MQMessage(); MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); mqQMgr = new MQQueueManager(qmName); mqQueue = mqQMgr.AccessQueue(qName, MQC.MQOO_BROWSE | MQC.MQGMO_LOGICAL_ORDER // open queue for browse + MQC.MQOO_FAIL_IF_QUIESCING); mqGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST; mqQueue.Get(mqMsg, mqGetMsgOpts); MessageBox.Show(mqMsg.ReadString(mqMsg.TotalMessageLength));
Thanx, Gee -
Hello, Can anyone help me, how can i browse the all messages in the queue? i have found several examples to get or put the messages, but about the browsing i haven't. The folloing example is browse the first messages, but i would like to see the all messages...
MQQueueManager mqQMgr; MQQueue mqQueue; MQMessage mqMsg = new MQMessage(); MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); mqQMgr = new MQQueueManager(qmName); mqQueue = mqQMgr.AccessQueue(qName, MQC.MQOO_BROWSE | MQC.MQGMO_LOGICAL_ORDER // open queue for browse + MQC.MQOO_FAIL_IF_QUIESCING); mqGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST; mqQueue.Get(mqMsg, mqGetMsgOpts); MessageBox.Show(mqMsg.ReadString(mqMsg.TotalMessageLength));
Thanx, GeeReplace MQC.MQGMO_BROWSE_FIRST with MQC.MQGMO_BROWSE_NEXT and call Get in a loop until it fails - it will throw a harmless exception with a reason code of MQRC_NO_MSG_AVAILABLE when there are no more messages to browse. I would also suggest that you use MQGMO_NO_WAIT rather than MQGMO_WAIT so that you don't have to wait until a message appears on the queue when you browse an empty queue. Example code using home-brewed helper methods:
public bool browseMessages(out ArrayList messages, string queue)
{
// browse (i.e. get without removing) a message on the message queue
messages = new ArrayList();
MQQueueManager mqQM = null;
MQQueue mqQ = null;try { if (queue == null || queue.Length == 0) throw new Exception("Queue name required"); // connect to queue manager connectManager(out mqQM); // open queue with required access connectQueue(mqQM, queue, MQC.MQOO\_FAIL\_IF\_QUIESCING + MQC.MQOO\_BROWSE, out mqQ); // get message options MQGetMessageOptions mqGMO = new MQGetMessageOptions(); mqGMO.Options = MQC.MQGMO\_FAIL\_IF\_QUIESCING + MQC.MQGMO\_NO\_WAIT + MQC.MQGMO\_BROWSE\_NEXT; // browse with no wait mqGMO.MatchOptions = MQC.MQMO\_NONE; // no matching required MQMessage mqMsg = new MQMessage(); // object to receive the message // browse all messages on the queue while (true) { // browse message - exception will be thrown when no more messages mqQ.Get(mqMsg, mqGMO); // get message text from the message object messages.Add(mqMsg.ReadString(mqMsg.MessageLength)); } } catch (MQException mqex) { if (mqex.ReasonCode == MQC.MQRC\_NO\_MSG\_AVAILABLE) { // harmless exception MQ throws to indicate that there are no messages on the queue return true; } else throwMQException(mqex); } finally { if (mqQ != null) mqQ.Close(); if (mqQM != null) mqQM.Disconnect(); } return false; }
Gavin