0

I have the MQConsumerClass that retrieves the messages from the queue as follows.

while (running)
{
    try
    {
        MQMessage rcvMessage = new MQMessage();
        MQGetMessageOptions gmo = new MQGetMessageOptions();

        gmo.options = MQConstants.MQGMO_WAIT;
        gmo.waitInterval = 60000; 
        queue.get(rcvMessage, gmo);

        if(rcvMessage.getTotalMessageLength() > 0)
        {
            String msgText = rcvMessage.readUTF();
        }
    }
    catch blocks{}
}

But the problem is when there is no message in the queue, the 2033 exception is thrown.

I think 2033 NO_MSG_AVAILABLE exception is NOT serious exception that probably can stop the operation.

However, I want to call onMessage(Message msg) function whenever there is a new message in the queue.

I want to create this class with MessageListener but I cannot find MessageListener or any example related to it.

So, please tell me if there is any MessageListener function in WMQ Java API.

or

any way to do this?

Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38

4 Answers4

5

The 2033 'error' is really not an error, it is more of an MQSeries return status: MQRC_NO_MSG_AVAILABLE 2033 The MQTester program is polling a queue looking for a message, but there are no messages available.

You can exclude it with

MQException.logExclude(new Integer(MQException.MQRC_NO_MSG_AVAILABLE));

aliwa
  • 51
  • 1
  • 2
1

Well, WMQ Java API does not have a message listener. Similar to onMessage of JMS is not there, only synchronous receive.

Shashi
  • 14,980
  • 2
  • 33
  • 52
  • So, do you mean there is not choice other than while loop? To lessen number of while loops, I used MQConstants.MQGMO_WAIT; and set waitInterval to half minute(60,000). But, "MQJE001: Completion Code '2', Reason '2033'." is displayed if queue doesn't have any message till waitInterval is over. So, how can I avoid that exception? I edited my new codes. Thank you so much for your help. – Lwin Htoo Ko Jun 29 '12 at 07:09
1

You can't completely avoid these 2033 "no message" exceptions in this kind of loop. By increasing the wait interval, there are few downsides (other than perhaps slower application closedown when you are done) and you will reduce the chance of failing to get a message for each get call. So for example if you specify a 5 minute wait interval, so long as at least one message arrives on the queue every 5 minutes, then every get call will receive a message (in general). But you still need a catch block for that kind of exception as you should expect sometimes to fail to find a message within your wait interval. So long as your wait interval is not too short, the performance overhead of that failed get, catch block and respin around the loop, should be relatively small.

If you really want to use an asynchronous consumer where your application will be called when a message becomes available, then the WebSphere MQ classes for JMS is the Java API where you will find a MessageListener interface. WebSphere MQ exposes 2 different APIs for Java. Your code snippet is using WebSphere MQ classes for Java, rather than JMS.

Brian Cope
  • 113
  • 3
1

You can use MQConstants.MQWI_UNLIMITED to specify ulimited wait interval.

while (running) {
      MQGetMessageOptions gmo = new MQGetMessageOptions();
      gmo.options = MQConstants.MQGMO_WAIT; 
      gmo.waitInterval = MQConstants.MQWI_UNLIMITED;

  try {
      // Get the message off the queue.
      queue.get(rcvMessage, gmo);
  } catch (MQException e) {
    if (e.reasonCode == 2033) {
        continue;
  }

}

}

ejlp12
  • 374
  • 2
  • 4