Develop an enterprise application to use MDBs
Develop the message-driven beans to delegate the business processing of incoming messages to another enterprise bean, to provide clear separation of message handling and business processing. This 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.
- Create the Enterprise Application project
- Create the message-driven bean class.
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), 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. 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).
The following 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.The result of this step is a message-driven bean that can be assembled into an .EAR file for deployment.
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; }- 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 WAS.
After you have developed an enterprise application to use message-driven beans, configure and deploy the application