C # MQ Api How to get a message without exception in case of an empty queue

I need to periodically check messages in the queue in Websphere MQ. I did not find a better approach, instead of trying to get the message and process the reason code 2033 (which is NO_MSG_AVAILABLE) as follows:

try { // ... inQueue.Get(message); } catch (MQException exception) { if (exception.ReasonCode != 2033) throw; } 

Is there a better way to get a message from the queue? I think there may be some kind of openOptions flag that I don't know about, it will not throw an exception if the message is not available, but returns null instead.

+4
source share
3 answers

There are three ways to avoid or reduce this polling mechanism. Here they are in a different elegance (the higher the better):

  • MQGET with a wait interval of UNLIMITED and MQGMO_FAIL_IF_QUIESCING
  • Get your app using MQServer
  • Callback function - new with MQ V7 on both sides
+6
source

You are missing the MQC.MQGMO_WAIT flag on MQGetMessageOptions.Options . Change it as follows:

 getOptions = new MQGetMessageOptions {WaitInterval = MQC.MQWI_UNLIMITED, Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING} 

Please note that this will block the calling thread until the message arrives in the queue or any connection occurs. MQ has another client called the IBM Message Service Client (aka XMS.NET) that provides an implementation of the JMS specification in .NET. It has a cute little Message Listener that is automatically called whenever a message comes into the queue. Unlike the example above, the calling thread will not be blocked when using the Message Listener.

More about XMS.NET can be found here . Samples also come with MQ and for sample listening, please refer to the source file SampleAsyncConsumer.cs.

+4
source

I got it. I solved this by putting a message initiator inside the loop:

 _queueManager = new MQQueueManager(Queuemanager, _mqProperties); MQQueue queue = _queueManager.AccessQueue( Queuename, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE); string xml = ""; while (queue.CurrentDepth > 0) { MQMessage message = new MQMessage(); queue.Get(message); xml = message.ReadString(message.MessageLength); MsgQueue.Enqueue(xml); message.ClearMessage(); } 

There should be something inside the message that errors when reusing for another are received.

+2
source

All Articles