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.
There are two Message-Driven Beans in this example:
$JONAS_ROOT/examples/src/mdb/sampleappli/StockHandlerBean is a Message-Driven Bean listening to a topic and receiving Map messages. The onMessage method runs in the scope of a transaction started by the container. It sends a Text message on a Queue (OrdersQueue) and updates a Stock element by decreasing the stock quantity. If the stock quantity becomes negative, an exception is received and the current transaction is marked for rollback.
$JONAS_ROOT/examples/src/mdb/sampleappli/OrderBean is another Message-Driven Bean listening on the OrdersQueue Queue. On receipt of a Text message on this queue, it writes the corresponding String as a new line in a file (Order.txt).
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.
To compile the sample application examples/src/mdb/sampleappli, use Ant with the $JONAS_ROOT/examples/src/build.xml file.
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.
Run the JOnAS Server.
service jonas start |
Deploy the sampleappli container:
jonas admin -a sampleappli.jar |
Run the EJB client:
jclient sampleappli.SampleAppliClient |
Stop the server:
service jonas stop |