Show TOC

Procedure documentationCreating Message-Driven Beans in the Developer Studio Locate this document in the navigation structure

 

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

Prerequisites

An EJB 3.0 Project exists.

Procedure

  1. In the Project Explorer, select the EJB 3.0 Project.

  2. In the context menu of the EJB 3.0 project, choose   New   Other  .

  3. Choose   EJB   Message Driven Bean   on the New dialog. Choose Next.

  4. Enter the bean settings as required.

  5. Choose Finish.

Example

To specify that you are implementing 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.

Syntax Syntax

  1. 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);
    
          }
    
       }
    
    }
    
End of the code.
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 {}.