Show TOC Start of Content Area

Background documentation Developing the Message Driven Bean  Locate the document in its SAP Library structure

Use this procedure to create a message driven bean that receives asynchronously messages. This enterprise bean has one obligatory method, onMessage(), which comes from the message-listener interface.

Create the Message Driven Bean ..

...

       1.      In the Developer studio, go to the J2EE Explorer, select the project node MDBTest and open the context menu. Choose New EJB…

       2.      In the New EJB wizard screen that appears, assign the following values to the properties of the new EJB:

       Enter MDBEjb in the EJB Name field to specify the name of the message-driven bean.

       The EJB Project field specifies the project where we create the bean. In this case this is MDBTest.

       Select Message-Driven Bean in the Bean Type field to specify the type of the bean.

       Enter com.sap.jms.test in the Default EJBPackage field.

       3.      Choose Finish.

Specify the JMS connection factory and destination

...

       1.      Expand the project MDBTest and double-click the ejb-j2ee-engine.xml node.

       2.      In the Enterprise Beans screen that appears expand the message-driven beans node and choose the MDBEjbBean.

       3.      On the right side enter JMSTestQueue in the Destination name field and QueueConnFactory in the Connection factory name field.

Implementation of the methods

...

       1.      In the project structure, expand the MDBTestejb-jar.xml node and double-click the name MDBEjbBean.

       2.      In the overview that appears, choose Navigate to Bean Class.

The Java Editor opens and displays the source code of the generated bean class.

Use the following source code:

package com.sap.jms.test;

 

import javax.ejb.MessageDrivenBean;

import javax.ejb.MessageDrivenContext;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

import javax.naming.Context;

 

/*

 * The MessageBean class is a message-driven bean. It implements the

 * javax.ejb.MessageDrivenBean and javax.ejb.MessageListener interfaces.

 * It is defined as public (but not final or abstract). It defines a

 * constructor and the methods setMessageDrivenContext, ejbCreate, onMessage,

 * and ejbRemove.

 */

 

public class MDBEjbBean implements MessageDrivenBean, MessageListener {

 

   private transient MessageDrivenContext mdc = null;

   private Context context;

 

   /*

    * Constructor, which is public and takes no arguments.

    */

 

   public MDBEjbBean() {

      System.out.println("In MDBEjbBean.MDBEjbBean()");

   }

 

   /*

    * onMessage method, declared as public (but not final or static),

    * with a return type of void, and with one argument of type

    * javax.jms.Message.

    */

 

   public void onMessage(Message inMessage) {

      TextMessage msg = null;

 

      try {

         if (inMessage instanceof TextMessage) {

            msg = (TextMessage) inMessage;

            System.err.println(

               "MESSAGE BEAN: Message " + "received: " + msg.getText());

         } else {

            System.err.println(

               "Message of wrong type: " + inMessage.getClass().getName());

         }

      } catch (JMSException e) {

         System.err.println(

            "MDBEjbBean.onMessage: " + "JMSException: " + e.toString());

         mdc.setRollbackOnly();

      } catch (Throwable te) {

         System.err.println(

            "MDBEjbBean.onMessage: " + "Exception: " + te.toString());

      }

   }

 

   /*

    *  ejbRemove method, declared as public (but not final or static),

    *  with a return type of void, and with no arguments.

    */

 

   public void ejbRemove() {

      System.out.println("In MDBEjbBean.remove()");

      // TODO : Implement

   }

 

   /*

    *  setMessageDrivenContext method, declared as public

    * (but not final or static), with a return type of void,

    * and with one argument of type javax.ejb.MessageDrivenContext.

    */

 

   public void setMessageDrivenContext(MessageDrivenContext context) {

      myContext = context;

      System.out.println("In" + "MDBEjbBean.setMessageDrivenContext()");

 

   }

 

   private MessageDrivenContext myContext;

 

   /*

    * ejbCreate method, declared as public (but not final ot static),

    * with a return type of void, and with no arguments.

    */

 

   public void ejbCreate() {

      System.out.println("In MDBEjbBean.ejbCreate()");

      //TODO: Implement

   }

}

       3.      Save the content. The Developer Studio updates and compiles the project sources.

The MDB has been created in com.sap.jms.test.MDBEjbBean package. The package now contains the bean’s class and the bean’s home and remote interfaces. You have also completely implemented the required methods of the MDB.

Next Step

Creating the Enterprise Bean Archive

 

End of Content Area