- Introduction to Messaging
- Components of the JMS Architecture
- The Two JMS Message Models
- The JMS Interfaces
- The Details of a JMS Message
- Message Selection and Filtering
- Using the JMS Point-to-Point Model
- Using the JMS Publish/Subscribe Model
- Synchronous Versus Asynchronous Messaging
- Message Persistence
- Using Transactions with JMS
- Using JMS with Enterprise JavaBeans
- Troubleshooting
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:
receiveReceive the next message for this consumer
receive(long timeout)Receive the next message that arrives within the timeout interval
receiveNoWaitReceive 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.