The following tutorial illustrates how a Message Driven Bean is written and deployed in an Enterprise JavaBeansTM 2.0 Container. The MDB component is invoked by an inbound message from a Java client. This function is demonstrated with a sample application run on Pramati Server 3.0 (Alpha). The Server ships with Pramati Message Server 1.0 and can be downloaded from www.pramati.com. The application sources are freely downloadable to get a better understanding of how MDB components are written and work.
What is a Message Driven Bean?
A message driven bean is a stateless, server-side, transaction-aware component that is driven by a Java message (javax.jms.message). It is invoked by the EJB Container when a message is received from a JMS Queue or Topic. It acts as a simple message listener.
A Java client, an enterprise bean, a Java ServerPagesTM (JSP) component, or a non-J2EE application may send the message. The client sending the message to the destination need not be aware of the MDBs deployed in the EJB Container. However, the message must conform to JMS specifications.
Before MDBs were introduced, JMS described a classical approach to implement asynchronous method invocation. The approach used an external Java program that acted as the listener, and on receiving a message, invoked a session bean method.
However, in this approach the message was received outside the application server and was thus not part of a transaction in the EJB Server. MDB solves this problem.

Message processing before (above) and after (below) Message Driven Beans.

Structure of an MDB
- It has no home or remote interfaces, and is only a bean class.
- It resembles a stateless session bean - that is, it has short-lived instances and does not retain state for a client.
- A client interacts with the MDB in the same way it interacts with a JMS application or JMS server.
- Through the MDB, the EJB 2.0 Container sets itself up as a listener for asynchronous invocation and directly invokes the bean (no interfaces), which then behaves like an enterprise bean.
- All instances of a particular MDB type are equivalent as they are not directly visible to the client and maintain no conversational state. This means that the Container can pool instances to enhance scalability.
Lifecycle of an MDB
The EJB Container performs several tasks at the beginning of the life cycle of the MDB:
- Creates a message consumer (a
QueueReceiverorTopicSubscriber) to receive the messages
- Associates the bean with a destination and connection factory at deployment
- Registers the message listener and the message acknowledgement mode
The lifecycle of an MDB depends on the lifespan of the EJB Server in which it is deployed. As MDBs are stateless, bean instances are typically pooled by the EJB Server and retrieved by the Container when a message becomes available on the topic or queue.
Writing an MDB
Writing an MDB involves the following tasks:
- Implement the
javax.ejb.MessageDrivenBean






