| CONTENTS | PREV | NEXT | INDEX | Designing Enterprise Applications with the J2EETM Platform, Second Edition |
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.
Consider using message-driven beans under the following circumstances:
- When you need to have asynchronous messaging in your application
- When you want to have messages automatically delivered. Automatic message delivery avoids polling for messages.
- When you want to integrate two applications in a loosely-coupled but reliable manner. Because JMS interfaces are available for most leading message-oriented middleware products, message-driven beans are widely used to integrate enterprise beans with packages and legacy applications. Chapter 6 discusses this approach further.
- When you want the message delivery to drive other events in the system. For example, workflow steps can be based on the mere fact of message delivery, or they can be based on the message content.
- When you want to create message selectors. A message selector is designed to take on only specific messages, thus making it possible to use message-driven beans as triggers.
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 |