Developing an enterprise application to use message-driven beans

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

Why and when to perform this task

You are recommended to develop the message-driven bean to 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 about writing the message-driven bean class, see Creating a message-driven bean in the WebSphere Studio help bookshelf.

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

Steps for this task

  1. Creating the Enterprise Application project, as described in the WebSphere Studio article .
  2. Creating the message-driven bean class.

    You can 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()

      You must define and implement an ejbCreate method for each way in which you want a new instance of an enterprise bean to be created.

    • 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 execute 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. Assembling and packaging the application for deployment.

    You can use WebSphere Studio to assemble and package the application for deployment.

Results

The result of this task is an .EAR file, containing an application message-driven bean, that can be deployed in WebSphere Application Server.

What to do next

After you have developed an enterprise application to use message-driven beans, configure and deploy the application; for example, define the listener ports for the message-driven beans and, optionally, change the deployment descriptor attributes for the application. For more information about configuring and deploying an application that uses message-driven beans, see Deploying an enterprise application to use message-driven beans

Related tasks
Deploying an enterprise application to use message-driven beans
Using message-driven beans in applications
Securing enterprise bean applications using the Assembly Toolkit[Version 5.0.2 and later]
Securing enterprise bean applications using the Application Assembly Tool[5.0 only][Version 5.0.1][Version 5.0.2]



Searchable topic ID:   tmb_devap
Last updated: Jun 21, 2007 9:56:50 PM CDT    WebSphere Application Server for z/OS, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.websphere.zseries.doc/info/zseries/ae/tmb_devap.html

Library | Support | Terms of Use | Feedback