23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

In addition to providing the container infrastructure for message-driven beans, EJB 2.0<br />

provides another important advantage: concurrent processing. In EJB 2.0, a messagedriven<br />

bean is deployed as a JMS consumer. It subscribes to a topic or connects to a queue<br />

and waits to receive messages. At runtime, the EJB container actually instantiates many<br />

instances of the same message-driven bean and keeps those instances in pool. When a<br />

message is delivered to a message-driven bean, one instance of that bean is selected from a<br />

pool to handle the message. If several messages are delivered at the same time, the<br />

container can select a different bean instance to process each message; the messages can be<br />

processed concurrently. Because a message-driven bean can consume messages<br />

concurrently in a robust server environment, it is capable of much higher throughput and<br />

better scalability than most traditional JMS clients.<br />

<strong>Message</strong>-driven beans are composed of a bean class and an XML deployment descriptor.<br />

The bean class must implement both the javax.ejb.<strong>Message</strong>DrivenBean interface and the<br />

javax.jms.<strong>Message</strong>Listener interface. The <strong>Message</strong>DrivenBean interface defines three<br />

methods:<br />

package javax.ejb;<br />

import javax.jms.<strong>Message</strong>;<br />

public interface <strong>Message</strong>DrivenBean {<br />

public void ejbCreate( );<br />

public void ejbRemove( );<br />

public void set<strong>Message</strong>DrivenContext(<strong>Message</strong>DrivenContext mdc);<br />

}<br />

The <strong>Message</strong>Listener interface defines the on<strong>Message</strong>( ) method:<br />

package javax.jms;<br />

public interface <strong>Message</strong>Listener {<br />

public void on<strong>Message</strong>( );<br />

}<br />

The set<strong>Message</strong>DrivenContext( ) is called on each instance right after it is instantiated. It<br />

provides the instance with a <strong>Message</strong>DrivenContext, which is based on a standard container<br />

interface, EJBContext. The ejbCreate( ) method is invoked on each instance after the<br />

set<strong>Message</strong>DrivenContext( ) method, but before the bean instance is added to the pool for<br />

a particular message-driven bean. Once the message-driven bean has been added to the<br />

pool, it's ready to process messages. When a message arrives, the instance is removed from<br />

the pool and its on<strong>Message</strong>( ) method is invoked. When the on<strong>Message</strong>( ) method returns,<br />

the bean instance is returned to the pool and is ready to process another message. The<br />

ejbRemove( ) method is invoked on an instance when it is discarded. This might happen if<br />

the container needs to reduce the size of the pool. The lifecycle of a message-driven bean<br />

is shown in Figure 8.3.<br />

129

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!