user1047088
user1047088

Reputation:

The best way to check IBM WebSphere MQ 7.0 queue on incoming messages

I am novice in IBM WebSphere MQ and I would like to ask you about the best approach to solve the following task.

I use WebSphere MQ 7.0 and I have implemented an java app to check MQ queue on incoming messages.

Incoming queue opened via the following code:

int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE;
MQQueue incomingQueue = 
          qManager.accessQueue(qName, openOptions, null, null, null);

Now, the task is to check in real-time mode when new messages appear in incomingQueue and process them.

I permanently check queue depth via invocation of incomingQueue.getCurrentDepth() in while-loop and check if it is bigger than zero then I get new messages.

That works, but I believe it is not a good approach.

What is the best approach to be notified when a new incoming message appeared in MQ Queue?

Thank you.

Upvotes: 3

Views: 2810

Answers (2)

prodeveloper
prodeveloper

Reputation: 950

Try to use the below open options to access a queue

openOptions = MQConstants.MQOO_INQUIRE + MQConstants.MQOO_FAIL_IF_QUIESCING
                        + MQConstants.MQOO_INPUT_AS_Q_DEF + MQConstants.MQOO_READ_AHEAD;

And following get options to get the messages

MQGetMessageOptions getOptions = new MQGetMessageOptions();
            getOptions.options = MQConstants.MQGMO_WAIT + MQConstants.MQGMO_PROPERTIES_COMPATIBILITY
                    + MQConstants.MQGMO_ALL_SEGMENTS_AVAILABLE + MQConstants.MQGMO_COMPLETE_MSG
                    + MQConstants.MQGMO_ALL_MSGS_AVAILABLE;

MQConstants.MQGMO_WAIT option will help us to read the messages when arrives to the queue. But make sure a Java thread/program should be there to run your class all the time to listen to the queue

Upvotes: 0

Shashi
Shashi

Reputation: 15283

Just call the queue.Get(msg) method. This is a blocking call and will return only when there is a message on a queue.

If the above is not suitable as it is a blocking call, you could look at WMQ JMS that provides a message listener. The message listener is used to receive messages on a callback method while the main thread can continue to do other work.

There are good samples that comes with MQ. You can find them under (on Windows) \tools\jms\samples and tools\wmqjava\samples.

Upvotes: 2

Related Questions