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:
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. |