Article
Enterprise JavaBeans: A Primer
Message Driven Beans
A message-driven bean (MDB) is an enterprise bean that allows enterprise applications to handle messages asynchronously. An instance of an MDB is instantiated by an EJB container to act as a message listener. Messages received by an MDB may be sent by any JMS-compatible message transmitter, such as a JMS-enabled publish/subscribe topic.
Unlike session and entity beans, a client program does not direct invocations at an MDB. Instead, the MDB receives invocations from the message source through a standard callback method, onMessage.
The single parameter to the onMessage method is of type javax.jms.Message and it is up to the MDB programmer to cast the Message object to one of the five JMS message types: TextMessage, ObjectMessage, MapMessage, BytesMessage and StreamMessage.
An MDB can participate inside of a transaction context in that a message to the MDB can be rolled back and redelivered or purged, if necessary.
Figure 2 illustrates the relationships between a JMS message producer, an EJB container and a message-driven bean:

Figure 2
What Problems do EJBs Solve?
When considering the possible use of EJBs, you should take into account the following problems that EJBs can help to solve:
- Scalability – Enterprise beans are deployable across many different hardware platforms transparently, therefore they can be distributed as needed to take advantage of the available resources.
- Transaction Support – Enterprise beans support a declarative transaction model that is portable across operating systems. Transaction boundaries are handled by the EJB container and the transactions themselves can be handled by the EJB container.
- Client-Location Transparency – The EJB architecture is designed to hide the location of the EJBs from the client program. Therefore, EJB client programs can be developed without foreknowledge of the whereabouts of the target EJBs.
- Data Source Portability – Entity beans can be programmed in a way that leaves the data source-access specifics up to the EJB container.
- Reusability – Since the EJB architecture facilitates a high-degree of componentization, business logic can be written once and reused easily as needed.
- Asynchronous Messaging – In applications requiring asynchronous communications, message-driven beans can be employed.
Writing and Deploying an EJB
The following four steps illustrate the typical process to develop and deploy an EJB:
- Write the classes and interfaces for your enterprise bean
- Write a deployment descriptor
- Package the enterprise bean and associated files inside of a jar file
- Deploy the bean
A session bean and an entity bean are composed of a minimum of three classes/interfaces:
1. An interface that extends javax.ejb.EJBObject (referred to as the Component Interface), as the following example illustrates:
package com.jeffhanson.ejb;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface SimpleMessenger extends EJBObject
{
public String retrieveMessage() throws RemoteException;
}
2. An interface that extends javax.ejb.EJBHome (referred to as the Home Interface), as the following example illustrates:
package com.jeffhanson.ejb;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface SimpleMessengerHome extends EJBHome
{
SimpleMessenger create() throws RemoteException, CreateException;
}