Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.
Hannah Smalltree, Editorial DirectorA 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 the standard callback method, onMessage(), in the same manner as a JMS message listener.A Message Driven Bean must implement two interfaces:
- javax.jms.MessageListener - This interface defines the onMessage callback method. When a message is put on the queue/topic, the onMessage method of the message-driven bean is called by the EJB container and passed the actual message.
- javax.ejb.MessageDrivenBean - This is the EJB interface that contains the EJB lifecycle methods:
- ejbCreate() - called by the EJB container when the message-driven bean is created
- ejbRemove() - called by the EJB container when the message-driven bean is destroyed or removed from the EJB pool
- setMessageDrivenContext(MessageDrivenContext context) - called prior to ejbCreate and passed the message-driven context by the EJB container. The context has runtime information such as transaction data.
A message-driven bean must declare deployment information about itself in a deployment-descriptor file named ejb-jar-xml. The EJB container handles the duties of subscribing the bean to the topic or connecting it to the queue based on information placed in the deployment descriptor.
The ejb-jar.xml file contains:
The following is an example of a typical ejb-jar.xml file:
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>MyMDB</ejb-name>
<ejb-class>com.jeffhanson.ejb.MyMDB</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Topic</destination-type>
</message-driven-destination>
<security-identity>
<run-as-specified-identity>
<role-name>system</role-name>
</run-as-specified-identity>
</security-identity>
</message-driven>
</enterprise-beans>
</ejb-jar>
Message-driven beans are not located by client classes and client
classes do not directly invoke methods on them. All access to a
message-driven bean is through a JMS topic or queue which directs
messages at the message-driven bean through the EJB container. The EJB
container ultimately passes the JMS message to the message-driven bean
through the bean's onMessage method. All message-driven beans must
implement the javax.ejb.MessageDrivenBean and javax.jms.MessageListener
interfaces, as the following example illustrates:
public class MyMDB
implements MessageDrivenBean, MessageListener
{
private transient MessageDrivenContext ctx = null;
public MyMDB()
{
}
public void setMessageDrivenContext(MessageDrivenContext ctx)
{
this.ctx = ctx;
}
public void ejbCreate()
{
}
public void onMessage(Message message)
{
System.out.println("MDB Message received: "
+ message.toString());
}
public void ejbRemove()
{
}
}
This was first published in October 2004