Developing an enterprise application to use message-driven beans
Applications can use message-driven beans as asynchronous message consumers. You deploy a message-driven bean as a message listener for a destination. The message-driven bean is invoked by an activation specification or a JMS listener when a message arrives on the input destination that is being monitored.
About this task
You develop an enterprise application to use a message-driven bean as with any other enterprise bean, except that a message-driven bean does not have a home interface or a remote interface.
You should develop your message-driven bean to delegate the business processing of incoming messages to another enterprise bean, which provides clear separation of message handling and business processing. This separation also means that the business processing can be invoked either by the arrival of incoming messages or, for example, by a WebSphere® J2EE client. Responses can be handled by another enterprise bean acting as a sender bean, or they can be handled in the message-driven bean.
EJB 2.0 message-driven beans support only Java™ Message Service (JMS) messaging. EJB 2.1 and EJB 3 message-driven beans can support other messaging types in addition to JMS. 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.
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.
- onMessage(message),
which must meet the following
requirements:
- The method must have a single argument of type javax.jms.Message.
- The
throws
clause must not define any application exceptions. - If the message-driven bean is configured to use bean-managed transactions, it must call the javax.transaction.UserTransaction interface to scope the transactions. Because these calls occur inside the onMessage() method, the transaction scope does not include the initial message receipt. For more information, see the topic about message-driven beans transaction support.
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 the onMessage() method might be needed. For information about the message listener interface needed, see the documentation that was provided with your JCA-compliant resource adapter.
- ejbCreate()
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.
- ejbRemove()
This method is invoked by the container when a client invokes the remove method inherited by the enterprise bean 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).
- ejbTimeout(Timer)
This method is needed only to support notifications from the timer service, and contains the business logic that handles time events received.
Procedure
Results
Example
The following example shows how to create the message-driven bean class. The 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;
}