Example: Creating the message-driven bean class

All message-driven beans must implement the MessageDrivenBean interface. For JMS messaging, a message-driven bean must also implement the message listener interface javax.jms.MessageListener. Other Java™ EE Connector Architecture (JCA)-compliant resource adapters might provide their own message listener interfaces that must be implemented.

You can use the New Enterprise Bean wizard of Rational® 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, javax.jms.MessageListener

A message-driven bean can be registered with the EJB timer service for time-based event notifications if it also implements the javax.ejb.TimedObject interface, and invokes the timer callback method by the following call: void ejbTimeout(Timer). At the scheduled time, the container then calls the message-driven bean ejbTimeout method.

The message-driven bean class must define and implement the following methods:

For example, the following example code shows how to access the text and the JMS MessageID, from a JMS message of type TextMessage. In this example, first the onMessage() method of a message-driven bean is used to unpack the incoming text message and to extract the text and message identifier; then a private putMessage method (defined within the same message bean class) is used to put the message onto another queue:

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



Related tasks
Developing an enterprise application to use message-driven beans
Reference topic    

Terms of Use | Feedback

Last updated: Oct 21, 2010 3:36:59 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-express-iseries&topic=rmb_mdbclass
File name: rmb_mdbclass.html