17.4 Message-Driven Beans

Message-driven beans are quite similar to stateless session beans. Both are stateless, and with each method call, the container establishes a transaction context based on the deployment-descriptor transaction attribute for the message-listener methods.

Message-driven beans implement the MessageDrivenBean interface for lifecycle callbacks and a message-listener interface for business methods that is specific to the type of message provider with which the bean is used. Message-driven beans used with the JMS MessageListener interface have only one business method, onMessage( ), that takes one parameter: an instance of javax.jms.Message. Those that are used with another message provider must implement all of the methods of the corresponding message-listener interface. The interaction with JDO is the same in all cases.

The lifecycle of a message-driven bean (shown in Figure 17-3) is as simple as a stateless session bean. To use JDO with message-driven beans, your application uses the setMessageDrivenContext( ) method to save the context and look up and save the PersistenceManagerFactory.

Figure 17-3. The lifecycle of a message-driven bean

To process the message-listener method, your application code obtains a PersistenceManager from the PersistenceManagerFactory and handles the message, performing JDO accesses as required. At the end of the business method, you close the PersistenceManager.