Developing an enterprise application to use message-driven beans
Overview
The message-driven bean is invoked by a JMS listener when a message arrives on the input queue that the listener is listening.
It is 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.
One develops 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.
To develop an enterprise application to use a message-driven bean, complete the following steps:
- Create the Enterprise Application project
- Create the message-driven bean class.
One can use the New Enterprise Bean wizard of WebSphere Studio 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, MessageListenerThe message-driven bean class must define and implement the following methods:
- onMessage(message)
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. This means the appserver is given one attempt to process the message.
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.)
- 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'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).
For example, the following code extract shows how to access the text and the JMS MessageID, from a JMS message of type TextMessage:
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 onMessage() 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.
The result of this step is a message-driven bean that can be assembled into an .EAR file for deployment.
- Assemble and package the application for deployment.
The result of this task is an .EAR file, containing an application message-driven bean, that can be deployed in WebSphere Application Server.
After you have developed an enterprise application to use message-driven beans, configure and deploy the application; for example, define the listener ports for the message-driven beans and, optionally, change the deployment descriptor attributes for the application.
Deploying an enterprise application to use message-driven beans
Using message-driven beans in applications
Securing enterprise bean applications
WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.
IBM is a trademark of the IBM Corporation in the United States, other countries, or both.