How do I use message-driven bean (MDB)?, part 2

How do I use message-driven bean (MDB)?
Continued from part 1.

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 the standard callback method, onMessage(), in the same manner as a JMS message listener.

A Message Driven Bean must implement two interfaces:

  1. 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.
  2. 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 fully-qualified class name of the message-driven bean
  • A name for the message-driven bean
  • The destination type of the bean
  • Transaction attributes
  • Security information

    The following is an example of a typical ejb-jar.xml file:

    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

    Dig Deeper on Java Web Services



    Find more PRO+ content and other member only offers, here.

    Have a question for an expert?

    Please add a title for your question

    Get answers from a TechTarget expert on whatever's puzzling you.

    You will be able to add details on the next page.



    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to: