CONTENTS | PREV | NEXT | INDEX Designing Enterprise Applications
with the J2EETM Platform, Second Edition



5.6 Message-Driven Beans

A message-driven bean is a new type of enterprise bean introduced in EJB 2.0. A message-driven bean allows J2EE applications to receive JMS messages asynchronously. (Asynchronous messaging allows applications or components to communicate with other applications or components by exchanging messages in such a way that senders are independent of receivers. The sender sends its message and does not need to wait for the receiver to receive or process the message.)

Message-driven beans are components that receive inbound messages from a JMS provider. The primary responsibility of a message-driven bean is to process messages, because the bean's container automatically manages other aspects of the message-driven bean's environment. Message-driven beans contain business logic for handling received messages. A message-driven bean's business logic may key off the contents of the received message, or it may be driven by the mere fact of receiving the message. Its business logic may include such operations as:

Message-driven beans function as message listeners, consuming messages from a JMS destination (queue or a topic). While message-driven beans are currently limited to JMS messages, it is expected that their capabilities will be expanded in future EJB specifications to allow other messaging systems to be supported as well.

From a bean developer's standpoint, message-driven beans are much like stateless session beans, only simpler. They have the same life cycle as stateless session beans, but they do not have a component or home interface. The developer needs to be concerned with implementing only one business method for a message-driven bean, the onMessage method. The onMessage method contains the business logic that the message-driven bean executes upon receipt of a message. The bean typically examines the message and executes the actions necessary to process it. This may in turn involve the invocation of other components. Like session beans, message-driven beans may be used to drive workflow processes. In this case, however, it is the arrival of a particular message that causes the processing to be initiated.

A bean developer can choose to make the message-driven bean invocation part of a transaction. This can be done only when using container-managed transaction demarcation. When a message-driven bean is part of a transaction, then the message delivery is part of the subsequent transactional work. If the subsequent transaction fails, then the message delivery is rolled back along with the other transactional work. The message remains available in the JMS destination until picked up by another message-driven bean instance. Note that the message sender and message receiver, which is the message-driven bean, do not share the same transaction. Thus, the sender and receiver communicate in a loosely coupled but reliable manner.

Bean-managed transaction demarcation can also be used with a message-driven bean, but because the transaction is started within the onMessage method, the message delivery itself is not part of the transaction.


5.6.1 Uses of Message-Driven Beans

Consider using message-driven beans under the following circumstances:


5.6.2 Example: Invoice Message-Driven Bean

Code Example 5.5 shows a message-driven bean that updates purchase orders based on invoice information received in a JMS message. The message-driven bean listens for JMS messages containing invoice data. When it receives a message, its onMessage method extracts the invoice data from the message text. In this example, the data has been encoded in XML, which is parsed and then used to process an invoice.

public class InvoiceMDB implements 
			MessageDrivenBean, MessageListener {
	public void onMessage(Message msg) {
		try {
			String msgTxt = ((TextMessage) msg).getText();
			Invoice invoice = Invoice.fromXML(msgText);
			// do further processing
		} catch (...) { 
			// handle exceptions
		}
	}
}
Code Example 5.5 Message-Driven Bean Example


CONTENTS | PREV | NEXT | INDEX
Copyright © 2002 Sun Microsystems, Inc. All Rights Reserved.