If you issue a getMessage command when a queue is empty, the queue throws a Java code base Except_Q_NoMatchingMsg exception or returns a C code base MQERETURN_QUEUE_ERROR, MQEREASON_NO_MATCHING_MSG. This allows you to create an application that reads all the available messages on a queue.
Encasing the getMessage() call inside a try..catch block allows you to test the code of the resulting exception. This is done using the code() method of the MQeException class. You can compare the result from the code() method with a list of exception constants published by the MQe class. If the exception is not of type Except_Q_NoMatchingMsg, throw the exception again.
The following code shows this technique:
try { while(true) { /* keep getting messages until an exception is thrown */ MQeMsgObject msg = qmgr.getMessage( "myQMgr", "myQueue", null, null, 0 ); processMessage(msg); } } catch (Exception e) { if ( e.code() != MQe.Except_Q_NoMatchingMsg ) throw e; }
Therefore, you can read all messages from a queue by iteratively getting messages until MQe.Except_Q_NoMatchingMsg is returned.