Ask the Expert

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

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

    Requires Free Membership to View

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:

    <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

  • There are Comments. Add yours.

     
    TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

    REGISTER or login:

    Forgot Password?
    By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
    Sort by: OldestNewest

    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: