9.6. Message-Driven Beans Example

JOnAS provides examples that are located in the examples/src/mdb install directory.

samplemdb is a very simple example, the code of which is used in the previous topics for illustrating how to use Message-Driven Beans.

sampleappli is a more complex example that shows how the sending of JMS messages and updates in a database via JDBC may be involved in the same distributed transaction.

The following figure illustrates the architecture of this example application.

Figure 9-1. Example Architecture

There are two Message-Driven Beans in this example:

The example also includes a CMP Entity Bean $JONAS_ROOT/examples/src/mdb/sampleappli/Stock that handles a stock table.

A Stock item is composed of a Stockid (String), which is the primary key, and a Quantity (int). The method decreaseQuantity(int qty) decreases the quantity for the corresponding stockid, but can throw a RemoteException Negative stock.

The JMS Client application SampleAppliClient sends several messages on StockHandlerTopic (see $JONAS_ROOT/examples/src/mdb/sampleappli/SampleAppliClient.java). It uses Map messages with three fields: CustomerId, ProductId, and Quantity. Before sending messages, this client calls the EnvBean for creating the StockTable in the database with known values in order to check the results of updates at the end of the test (see $JONAS_ROOT/examples/src/mdb/sampleappli/EnvBean.java). Eleven messages are sent, the corresponding transactions are committed, and the last message sent causes the transaction to be rolled back.

9.6.1. Compiling This Example

To compile the sample application examples/src/mdb/sampleappli, use Ant with the $JONAS_ROOT/examples/src/build.xml file.

9.6.2. Running This Example

The default configuration of the JMS service in jonas.properties is:

jonas.services                 jmx,security,jtm,dbm,jms,ejb            
                               // The jms service must be added
jonas.service.ejb.descriptors  sampleappli.jar
jonas.service.jms.topics       StockHandlerTopic
jonas.service.jms.queues       OrdersQueue
jonas.service.jms.collocated   true

This indicates that the JMS Server will be launched in the same JVM as the JOnAS Server, and the JMS-administered objects StockHandlerTopic (Topic) and OrdersQueue (Queue) will be created and registered in JNDI, if it does not already exist.

  1. Run the JOnAS Server.

    service jonas start
  2. Deploy the sampleappli container:

    jonas admin -a sampleappli.jar
  3. Run the EJB client:

    jclient sampleappli.SampleAppliClient
  4. Stop the server:

    service jonas stop