Develop an enterprise application to use message-driven beans

Use this task to develop an enterprise application to use a message-driven bean. A message-driven bean is invoked by a JMS listener when a message arrives on an input queue or topic that the listener is monitoring.

It is recommended that the message-driven bean delegate the business processing of incoming messages to another enterprise bean, to provide clear separation of message handling and business processing. This also enables the business processing to be invoked by either the arrival of incoming messages or, for example, from a WebSphere J2EE client. Responses can be handled by another enterprise bean acting as a sender bean, or handled in the message-driven bean.

You develop an enterprise application to use a message-driven bean like any other enterprise bean, except that a message-driven bean does not have a home interface or a remote interface.

This topic describes how to develop a completely new message-driven bean class. If you have a WAS 4.0 enterprise application that uses the JMS listener, you can migrate that application to use message-driven beans. For more information, see Migrate a JMS listener application to use message-driven beans.

To develop an enterprise application to use a message-driven bean, complete these steps:

  1. Create an Enterprise Application project, as described in the WebSphere Studio documentation.
  2. Create the message-driven bean class.

    Use the New Enterprise Bean wizard of WebSphere Studio Application Developer to create an enterprise bean with a bean type of Message-driven bean. The wizard creates appropriate methods for the type of bean.

    By convention, the message bean class is named nameBean, where name is the name you assign to the message bean; for example:

    public class MyJMSppMDBBean implements MessageDrivenBean, MessageListener

    The message-driven bean class must define and implement the following methods:

    • onMessage(message), which must meet the following requirements:

      • The method must have a single argument of type javax.jms.Message.
      • The throws clause must not define any application exceptions.
      • If the message-driven bean is configured to use bean-managed transactions, it must call the javax.transaction.UserTransaction interface to scope the transactions. Because these calls occur inside the onMessage() method, the transaction scope does not include the initial message receipt. This means the application server is given one attempt to process the message.

      To handle the message within the onMessage() method (for example, to pass the message on to another enterprise bean), you use standard JMS. (This is known as bean-managed messaging.)

    • ejbCreate()
      Define and implement one or more ejbCreate methods.
    • ejbRemove().
      This method is invoked by the container when a client invokes the remove method inherited by the enterprise bean's home interface from the javax.ejb.EJBHome interface. This method must contain any code that you want to run before an enterprise bean instance is removed from the container (and the associated data is removed from the data source).

    For example, the following code extract shows how to access the text and the JMS MessageID, from a JMS message of type TextMessage:

    public void onMessage(javax.jms.Message msg)
    {
            String text      = null;
            String messageID = null;
    
            try
            {
                    text = ((TextMessage)msg).getText();
    
                    System.out.println("senderBean.onMessage(), msg text2: "+text);
    
                    //
                    // store the message id to use as the Correlator value
                    //
                    messageID = msg.getJMSMessageID();
    
                    // Call a private method to put the message onto another queue
                    putMessage(messageID, text);
            }
            catch  (Exception err)
            {
                    err.printStackTrace();
            }
            return;
    }
    

    Code example: The onMessage() method of a message bean. This figure shows a code extract for a basic onMessage() method of a sample message-driven bean. The method unpacks the incoming text message to extract the text and message identifier and calls a private putMessage method (defined within the same message bean class) to put the message onto another queue.

    The result of this step is a message-driven bean that can be assembled into an .EAR file for deployment.

  3. Assemble and package the application for deployment. Use WebSphere Studio to assemble and package the application for deployment.