Apache Geronimo Communication Basics-development, deployment, and testing (II.)
Message-driven bean ()
Now that you have implemented the PURCHASEORDEREJB, you can then implement Purchaseordermdb. Unlike an EJB, an MDB is a JMS listener that listens for JMS messages on or on a queue. Based on the EJB 2.1 specification, an MDB is not limited to a JMS listener, but can implement any custom listener interface. In part 3rd of this series, you will revisit this and modify Purchaseordermdb to integrate it with the JCA resource adapter using a custom listener interface.
In this tutorial, you will implement Purchaseordermdb, which listens for JMS messages that are received by a JMS topic (potopic) with a purchase request. When a purchase request message is received, it invokes PURCHASEORDEREJB to create a new purchase order.
Implement Purchaseordermdb
Unlike entity Bean,mdb, you do not have to implement a remote interface or a primary interface, but rather implement the Messagedrivenbean and MessageListener interfaces--and therefore must implement the OnMessage () method. Listing 9 shows the code snippet for Purchaseordermdb.
Purchaseordermdb source files (. java) can be found in the $part 1.home/src/examples/po/mdb directory.
Listing 9. MDB Source Code
public class PurchaseOrderMDB
implements MessageDrivenBean, MessageListener {
private MessageDrivenContext ctx = null;
public void setMessageDrivenContext(MessageDrivenContext mdCtx)
throws EJBException {
ctx = mdCtx;
}
public void onMessage(Message msg) {
if (msg instance of ObjectMessage) {
Serializable bean = ((ObjectMessage) msg).getObject();
poBean = (PurchaseOrderBean) bean;
addPurchaseOrder(poBean);
}
}// end onMessage
In the OnMessage () method of Purchaseordermdb, you called the PURCHASEORDEREJB Addpurchaseorder () method to create a new purchase order line in the database table.
The JNDI properties for the Geronimo server are as follows:
java.naming.factory.initial=org.openejb.client.RemoteInitialContextFactory
java.naming.provider.url=localhost:4201(4201 is the default port)
Java.naming.security.principal=system (Default user name)
Java.naming.security.credentials=manager (default password)