Home

 

Message-driven EJBs

Message-driven beans are used for the processing of asynchronous JMS messages within J2EE based applications. They are invoked by the container on the arrival of a message.

In this way, they can be thought of as another interaction mechanism for invoking EJBs, but unlike session beans, the container is responsible for invoking them when a message is received, not a client (or another bean).

To define a message driven bean in EJB 3.0, you declare a POJO:

@MessageDriven(activationConfig = {
			@ActivationConfigProperty(propertyName="destinationType",
											propertyValue="javax.jms.Queue"),
			@ActivationConfigProperty(propertyName="destination",
											propertyValue="queue/myQueue")
})
public class MyMessageBean implements javax.jms.MessageListener {
	public void onMessage(javax.msg.Message inMsg) {
		// implement the onMessage method
		// to handle the incoming message
		....
	}
}

Here are the main relevant features of this example:

In EJB 3.0, the MDB bean class is annotated with the @MessageDriven annotation, which specifies a set of activation configuration parameters. These parameters are unique to the particular kind of JCA 1.5 adapter that is used to drive the MDB. Some adapters have configuration parameters that let you specify the destination queue of the MDB. In the case where the adapter does not support this, the destination name must be specified using a <message-destination> entry in the XML binding file.

The bean class has to implement the MessageListener interface, which defines only one method, onMessage. When a message arrives in the queue monitored by this MDB, the container calls the onMessage method of the bean class and passes the incoming message in as the parameter.

Furthermore, the activationConfig property of the @MessageDriven annotation provides messaging system-specific configuration information.
ibm.com/redbooks