Show TOC Start of Content Area

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

In this step you create a message driven bean (MDB) that receives messages asynchronously. This enterprise bean has one obligatory method called onMessage(), which comes from the MessageListener interface.

...

       1.      In the context menu of the EJB 3.0 Project, choose New… Other… EJB… EJB 3.0.

       2.      Select Message Driven Bean 3.0 and choose Next.

       3.      In the EJB Class Name specify the name of the MDB, such as MDBBean, and name the package, such as com.sap.jms.test.MDBEjbBean.

       4.      Set the JMS indicator and select javax.jms.Queue in the Destination Type field. The JMS destination type must correspond to the resource name in the jms-resources.xml. Choose Next.

       5.      In the Mapping Name field enter the MyVeryNewQueue. This is the destination on which your MDB listens for messages. Choose Finish.

Implementation of the methods

You created MDBBean.java in the test package in the ejbModule. Now you have to implement this Java file. Here is an example:

package com.sap.jms.test.MDBEjbBean;

 

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

 

/**

 * A Message-Driven Bean processing TextMessages sent to MyVeryNewQueue.

 */

@MessageDriven(mappedName = "MyVeryNewQueue", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })

public class MDBBean implements MessageListener {

 

   /**

    * Receives a single JMSMessage from the specified Queue.

    *

    * @param msg

    *            the message received from MyVeryNewQueue

    */

 

   public void onMessage(Message msg) {

      try {

         // expecting only TextMessages

         if (msg instanceof TextMessage) {

            TextMessage textMsg = (TextMessage) msg;

            System.out.println("MDBBean: Message received: "

                  + textMsg.getText());

         } else {

            System.out.println("MDBBean: Message of wrong type: "

                  + msg.getClass().getName());

         }

      } catch (JMSException jmsexc) {

         System.out.println("MDBBean: Message processing failed. " + jmsexc);

      }

   }

}

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

Result

The MDB has been created in the com.sap.jms.test.MDBEjbBean package. You have also completely implemented the required methods of the MDB.

Next Step

Configuring the JMS Resources

End of Content Area