Asynchronous delivery

 

An alternative to making calls to QueueReceiver.receive() is to register a method that is called automatically when a suitable message is available. The following fragment 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();
  queueReceiver.setMessageListener(listener);
 
  // main program can now continue with other application specific
  // behavior.

Use of asynchronous delivery with a QueueReceiver marks the entire Session as asynchronous. It is an error to make an explicit call to the receive methods of a QueueReceiver that is associated with a Session that is using asynchronous delivery.


uj24470_