Appendix E. Message-Driven EJB: Status Update Listeners Example


Session beans, as we’ve seen up to this point, are best suited for servicing client requests. Many enterprise systems, however, use a messaging layer to asynchronously pass requests from application to application. In Java, we use the Java Message Service abstraction to push/pull messages via Queues and Topics, and the integration of JMS with EJB is the message-driven bean.

In this example, we implement social networking status updates via a consumer/publisher Topic. Anyone listening on the Topic will receive incoming status updates, and we create 2 listeners: 1 simple one to log out to the command-line or logfile, and another to push the updates to Twitter.

Note: Set the environment variables below before running to take advantage of the Twitter updates while running the tests.


Additionally, in this example we show how the use of a single JVM for the test and the server can be used to enable shared memory locking (i.e., java.util.concurrent) to reliably test that asynchronous components have completed as expected.

Source Listing

Following is a full listing of all source code used in this runnable example.

Implementation Resources ...

Get Enterprise JavaBeans 3.1, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.