Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Synchronous Versus Asynchronous Messaging

A client can be set up to synchronously or asynchronously receive messages. With synchronous delivery, a client can request the next message from a MessageConsumer by using one of the receive methods. Three different receive methods can be used:

  • receive—Receive the next message for this consumer

  • receive(long timeout)—Receive the next message that arrives within the timeout interval

  • receiveNoWait—Receive the next message if one is immediately available

A JMS consumer can also receive messages asynchronously. The client registers an object that implements the MessageListener interface with the JMS server. When messages arrive for a particular MessageConsumer that has a MessageListener registered, the messages are delivered to the onMessage method. This is the example that you saw in Listing 10.2.

NOTE

The JMS specification is very clear about the fact that the client that implements the onMessage method defined in the MessageListener interface should never throw a RuntimeException. If the onMessage method does happen to throw a RuntimeException, it's very unpredictable how the JMS server will react. It might consider the MessageListener to be confused and stop delivering messages to it. You should catch such exceptions and attempt to divert the message to some type of error handler instead.

  • + Share This
  • 🔖 Save To Your Account