Use this task to develop an enterprise application to use a message-driven bean. The message-driven bean is invoked by a J2C activation specification or a JMS listener when a message arrives on the input destination that the listener is monitoring.
Why and when to perform this task
You are recommended to develop the message-driven bean to delegate the business processing of incoming messages to another enterprise bean, to provide clear separation of message handling and business processing. This also enables the business processing to be invoked by either the arrival of incoming messages or, for example, from a WebSphere J2EE client. Responses can be handled by another enterprise bean acting as a sender bean, or handled in the message-driven bean.
You develop an enterprise application to use a message-driven bean like any other enterprise bean, except that a message-driven bean does not have a home interface or a remote interface.
For more information about writing the message-driven bean class, see Creating a message-driven bean in the Rational Application Developer help bookshelf.
To develop an enterprise application to use a message-driven bean, complete the following steps:
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
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 JCA-compliant Resource Adapters may provide their own message listener interface that needs to be implemented.
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 the timer callback method void ejbTimeout(Timer). At the scheduled time, the container invokes the message-driven bean's ejbTimeout method. The message-driven bean class must define and implement the following methods:
To handle the message within the onMessage() method (for example, to pass the message on to another enterprise bean), you use standard JMS. (This is known as bean-managed messaging.)
If you are using a JCA-compliant
Resource Adapter with a different message listener interface, another method besides onMessage() may be needed. For information about the message listener interface needed, see the documentation that was provided with your JCA Resource Adapter.
You must define and implement an ejbCreate method for each way in which you want a new instance of an enterprise bean to be created.
This method is invoked by the container when a client invokes the remove method inherited by the enterprise bean's home interface from the javax.ejb.EJBHome interface. This method must contain any code that you want to execute before an enterprise bean instance is removed from the container (and the associated data is removed from the data source).
This method is needed only to support notifications from the timer service, and contains the business logic that handles time events received.
For example, the following code extract shows how to access the text and the JMS MessageID, from a JMS message of type TextMessage:
Figure 1. Code example: The onMessage() method of a message bean .
This figure shows a code extract for a basic onMessage() method of a sample message-driven bean. The method unpacks the incoming text message to extract the text and message identifier and calls a private putMessage method (defined within the same message bean class) 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; }The result of this step is a message-driven bean that can be assembled into an EAR file for deployment.
If needed, you can override the values of these properties later, after the enterprise application has been exported into an EAR file for deployment.
Under Activation Configuration, review the following properties:
This property applies only to message-driven beans that uses bean-managed transaction demarcation (Transaction type is set to Bean).
As defined in the EJB specification, clients cannot use using Message.acknowledge() to acknowledge messages. If a value of CLIENT_ACKNOWLEDGE is passed on the createxxxSession call, then messages are automatically acknowledged by the application server and Message.acknowledge() is not used.
A non-durable subscriber can only be used in the same transactional context (for example, a global transaction or an unspecified transaction context) that existed when the subscriber was created. For more information about this context restriction, see The effect of transaction context on non-durable subscribers.
JMSType='car' AND color='blue' AND weight>2500
The selector string can refer to fields in the JMS message header and fields in the message properties. Message selectors cannot reference message body values.
For more details about these properties, see Message-driven bean deployment descriptor properties.
Under WebSphere Bindings, select the JCA Adapter option then specify the bindings deployment properties:
ResultThe result of this task is an EAR file, containing the message-driven bean, for the enterprise application that can be deployed in WebSphere Application Server.
Related tasks
Deploying an enterprise application to use message-driven beans against JCA
1.5-compliant resources
Programming to use