392 WebSphere Application Server - Express V6 Developers Guide and Development Examples
10.4 Message Driven Beans
The Enterprise JavaBeans specification (EJB), version 2.0 introduced a new type
of EJB called the Message Driven Bean (MDB).
Message Driven Beans are
asynchronous message consumers that run within the context of an application
server’s EJB container. This enables the EJB container to provide additional
services to the Message Driven Bean during the processing of a message, such
as transactions, security, concurrency and message acknowledgement.
The EJB container is also responsible for managing the lifetime of the Message
Driven Beans and for invoking Message Driven Beans when a message arrives
for which a given Message Driven Bean is the consumer.
Message Driven Bean instances should not maintain any conversational state on
behalf of a client. This enables the EJB container to maintain a pool of Message
Driven Bean instances and to select any instance from this pool to process an
incoming message. However, this does not prevent a Message Driven Bean from
maintaining state that is not specific to a client, for instance, DataSource
references or references to another EJB.
WebSphere Application Server V6.0 is fully compliant with version 1.4 of the
J2EE specification, which requires application servers to support version 2.1 of
the EJB specification.
10.4.1 Message Driven Bean types
Version 2.0 of the EJB specification defined a single type of Message Driven
Bean that enabled the asynchronous delivery of messages through the Java
Message Service (JMS).
However, the integration of multiple JMS providers into application servers has
proven difficult. For various reasons, many application server vendors have only
provided support for one JMS provider within their product. Also, the fact that
Message Driven Beans within the EJB 2.0 specification only support the JMS
programming model was considered too restrictive. Several other messaging
providers exist that require similar functionality to Message Driven Beans within
the EJB container, such as the Java API for XML Messaging (JAXM).
Because of this, version 2.1 of the EJB specification expanded the definition of
Message Driven Beans to provide support for messaging providers other than
JMS providers. It does this by allowing a Message Driven Bean to implement an
interface other than the javax.jms.MessageListener interface. This message
listener interface is specific to the messaging provider in question. The type of
message listener interface that a Message Driven Bean implements determines
Chapter 10. Java Message Service 393
its type. Therefore, a Message Driven Bean that implements the
javax.jms.MessageListener interface is a
JMS Message Driven Bean.
10.4.2 Client view of a Message Driven Bean
Unlike session and entity beans, Message Driven Beans do not expose home or
component interfaces. A client is not able to locate instances of a Message
Driven Bean and invoke methods on it directly.
The only manner in which a client can interact with a Message Driven Bean is to
send a message to the destination or endpoint for which the Message Driven
Bean is the listener. The EJB container is responsible for invoking an instance of
the Message Driven Bean as a result of the arrival of a message. From the
clients perspective, the existence of the Message Driven Bean is completely
transparent. This is shown in Figure 10-12, where the client is only able to see
the messaging provider and the target destination.
Figure 10-12 Client view of a Message Driven Bean
10.4.3 Message Driven Bean implementation
A bean provider developing a Message Driven Bean must provide a Message
Driven Bean implementation class. This class must implement, directly or
indirectly, the javax.ejb.MessageDrivenBean interface and a message listener
interface. It must also provide an ejbCreate method implementation. These
aspects of message-driven implementation are discussed in the following
sections.
Message Driven Bean interface
The javax.ejb.MessageDrivenBean interface defines a number of callback
methods that allow the EJB container to manage the life cycle of each Message
Message
Messaging Provider
Destination/Endpoint
Client
Message
Application Server
EJB Container
MDB
MDB
MDB
MDB
MDB
Message
Messaging Provider
Destination/Endpoint
Client
Message
Application Server
EJB Container
Application Server
EJB Container
MDB
MDB
MDB
MDB
MDB
MDB
MDB
MDB
MDB
MDB

Get WebSphere Application Server - Express V6 Developers Guide and Development Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.