Asynchronous delivery

 

An application can call the MessageConsumer.receive() method to receive messages. As an alternative, an application can register a method that is called automatically when a suitable message is available. This is called asynchronous delivery of messages. The following code illustrates the mechanism:

import javax.jms.*;

public class MyClass implements MessageListener
{
  // The method that will be called by JMS when a message
  // is available.
  public void onMessage(Message message)
  {
    System.out.println("message is "+message);

    // application specific processing here
    .
    .
    .
  }
}
 .
 .
 .
  // In Main program (possibly of some other class)
  MyClass listener = new MyClass();
  messageConsumer.setMessageListener(listener);

  // main program can now continue with other application specific
  // behavior. 

Using asynchronous delivery with a message consumer marks the entire session as using asynchronous delivery. An application cannot call the receive() methods of a message consumer if the message consumer is associated with a session that is using asynchronous delivery.


uj25210_