Show TOC

Creating Message-Driven Beans in the Developer StudioLocate this document in the navigation structure

Prerequisites

An EJB project exists.

Context

Use the message-driven bean to asynchronously receive messages from a Java Message Service (JMS) destination.

According to the Java Connector Architecture (JCA) 1.5, the Enterprise Information Systems (EIS) are able to call functions provided by Java applications. The specification states that these functions are provided by message-driven beans (MDBs).

Resource adapters are responsible for listening to messages or calls and forwarding them to the MDBs. You can configure the connection between the resource adapters and the EIS only through the MDBs.

Procedure

  1. In the Project Explorer , select the EJB project. In the context menu, choose Start of the navigation path New Next navigation step Message-Driven Bean (EJB 3.x) End of the navigation path.
  2. Enter the bean settings as required, then choose Next .
  3. Specify additional settings of the message-driven bean.
  4. Choose Finish .

Example

To specify that you are implementing a message-driven bean, you have to annotate the class with the @MessageDriven annotation. Although not required, you can specify attributes of the annotation to further describe the bean type. In this example the mappedName and the activationConfig attributes are specified.

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);

                }

        }

}

         
Annotation Reference

Name

Use

Target

Annotation Attributes

@MessageDriven

Use it to specify the bean class as a message-driven bean.

TYPE

name - this is the name of the bean class. The default value is "" .

mappedName - this is a product-specific name of the bean. The default value is "" .

description - this is a description of what a bean does. The default value is "" .

messageListenerInterface - specifies the message listener interface. The default value is javax.jmsMessageListener .

activationConfig - specifies an array of activation configuration name-value pairs that configure the bean in its operational environment. The default value is {} .