Message-Driven EJB Basics

Message-driven EJBs are the integration between EJB and the JMS. Like other EJB types, message-driven EJBs live within an EJB container and benefit from EJB container services such as transactions, security, and concurrency control. However, a message-driven EJB does not interact directly with clients. Instead, message-driven EJBs are JMS message listeners. A client publishes messages to a JMS destination. The JMS provider and the EJB container then cooperate to deliver the message to the message-driven EJB (see Figure 10-1).

Figure 10-1. Message-Driven EJBs


Because message-driven EJBs do not have clients, they do not require home or remote interfaces. A message-driven EJB is a bean class that implements the javax.ejb.MessageDrivenBean and the javax.jms.MessageListener interfaces. The MessageDrivenBean interface includes only two methods: setMessageDrivenContext and ejbRemove. The MessageListener interface is even simpler: It contains only a single method, onMessage. In addition to implementing these three methods, the bean writer provides a single ejbCreate method with no parameters. One of the best features of message-driven EJBs is their simplicity: This single bean class has only four methods.

Message-driven beans include a single class, which implements the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces.

Message-Driven EJB Lifecycle

The lifecycle of a message-driven EJB is very similar to a stateless session bean. When the container creates a new message-driven EJB instance, it first calls the bean class's default (no-parameter) constructor. While EJBs may implement a constructor, it is preferable to place initialization code in the setMessageDrivenContext or ejbCreate methods. The container next calls the setMessageDrivenContext method and passes the EJB a javax.ejb.MessageDrivenContext. This context, like the SessionContext or EntityContext, is generally saved in a member variable and can later be used to make calls into the EJB container. Finally, the EJB container calls the ejbCreate() method.

Like stateless session beans, message-driven beans only have a single, no-argument ejbCreate method. Message-driven EJB instances do not have an associated identity and, like session beans, they are created at the container's discretion. Also, like stateless session beans, message-driven bean instances should not keep any conversational state in the bean class.

When a message-driven EJB is deployed into the EJB container, the ejb-jar.xml specifies a JMS queue or topic. The EJB container registers the message-driven bean instance as a JMS listener. When a message arrives on the associated JMS destination, the EJB container retrieves a bean instance and calls the bean's onMessage method with the new message. Like stateless session beans, message-driven EJBs are pooled to minimize object creation. We discuss the WebLogic EJB container's pooling of message-driven EJBs later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.166.37