Oracle GlassFish Message Queue 4.4.2 Developer's Guide for Java Clients

Receiving Messages Synchronously

Once you have created a message consumer for a session, using either the createConsumer orcreateDurableSubscriber method, you must start the session’s connection to begin the flow of incoming messages:


(Note that it is not necessary to start a connection in order to produce messages, only to consume them.) You can then use the consumer’s receive method to receive messages synchronously from the message broker:

Message  inMsg = myConsumer.receive();

This returns the next available message for this consumer. If no message is immediately available, the receive method blocks until one arrives. You can also provide a timeout interval in milliseconds:

Message  inMsg = myConsumer.receive(1000);

In this case, if no message arrives before the specified timeout interval (1 second in the example) expires, the method will return with a null result. An alternative method, receiveNoWait, returns a null result immediately if no message is currently available:

Message  inMsg = myConsumer.receiveNoWait();