MDB
Internal
Overview
A Message-Driven Bean (MDB) is an asynchronous message consumer, invoked by the container as a result of the arrival of a message at the JMS destination serviced by the message-driven bean.
To a client, an MDB is a message consumer that implements some business logic running on the server. A client accesses an MDB by sending messages to the JMS destination associated with the MDB container of that type. MDBs are anonymous. They have no client-visible identity. MDB instances have no conversational state either, meaning they do not maintain state for a specific client. All bean instances are equivalent when they are not involved in servicing a client message. An MDB instance is created by the container to handle the processing of the messages for which the MDB is the consumer. Its lifetime is controlled by the container.
API
The @MessageDriven Annotation
An MDB must be annotated with the @javax.ejb.MessageDriven annotation, or denoted in its deployment descriptor as a message driven bean.
activationConfig
messageListenerInterface
Code
An MDB must implement the appropriate listener interface for the messaging type the MDB supports. If the MDB implements javax.jms.MessageListener interface, that distinguishes the MDB as a JMS MDB. If the class does not implement the interface, the MDB must specify the message listener interface using the @MessageDriven annotation, with its "messageListenerInterface" element, or the messaging-type deployment descriptor element. The class must have a public constructor with no arguments.
The onMessage() method of the message listener is called by the container when a message arrives. The method must contain the business logic to handle the message.
It is the container's responsibility to insure that only one thread can be executing an instance at any time.
Deployment Descriptor
messaging-type
Examples
The simplest possible working MDB example is available here:
Another example that showcases most of the configuration and API details discussed in this article is available here:
Transactional Context
Security Context
Relationship with the Container and Lifecycle
It is the container's responsibility to instantiate the MDB instances, manage their life cycle, notify the MDB instances when bean action may be necessary and provide security, concurrency, transactions and other services. The MDB instances need not concern about scalability and the concurrent processing of a large number of messages: it is the container's responsibility to instantiate a large enough MDB instance number and handle the message distribution to them. All MDB instances are equivalent, a client message can be delivered to any available instance.
An MDB may use dependency injection mechanisms to acquire references to other objects in the environment. If the MDB uses dependency injection, the container will inject these reference after the bean instance is created and before the first invocation of the onMessage() method.
MessageDrivenContext
The MDB may declare interest in accessing services from the container by requesting a MessageDrivenContext to be injected into it (or, alternatively, by implementing the MessageDrivenBean interface):
@Resource private MessageDrivenContext context;
The MessageDrivenContext allows the MDB instance to:
- Get access to the javax.transaction.UserTransaction instance, which then can be used to interact with the on-going transaction and obtain the transaction status. Only MDBs that declared themselves to use bean managed transaction can use this method.
Client's Access to MDBs
Clients cannot access a specific MDB, they can only send messages to destinations serviced by MDBs.
A client looks up the destination in JNDI. The client's JNDI name space may be configured to include destinations serviced by MDBs installed in multiple containers located on multiple machines on a network. The actual locations of the MDBs are transparent to the clients sending messages into them.