Use this task to develop an enterprise application to use a message-driven bean. The message-driven bean is invoked by a JMS listener when a message arrives on the input queue 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.
This topic describes how to develop a completely new message-driven bean class. If you have a WAS 4.0 enterprise application that uses the JMS listener, you can migrate that application to use message-driven beans, as described in Migrating a WAS 4.0 JMS listener application to use message-driven beans.
For more information about writing the message-driven bean class, see Creating a message-driven bean in the WebSphere Studio help bookshelf.
To develop an enterprise application to use a message-driven bean, complete the following steps:
Steps for this task
You 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, MessageListener
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.)
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).
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; }
You can use WebSphere Studio to assemble and package the application for deployment.
Results
The result of this task is an .EAR file, containing an application message-driven bean, that can be deployed in WebSphere Application Server.What to do next
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. For more information about configuring and deploying an application that uses message-driven beans, see Deploying an enterprise application to use message-driven beans