Sample E-mail Reader Application
The reader application performs many of the same steps as the sender in terms of initializing its JMS connection and session. The differences are:
- The constructor creates a javax.jms.MessageConsumer object for the "Inbox" queue because its purpose is to receive, or consume, messages.
- The reader application implements the javax.jms.MessageListener interface and provides the resulting object to the JMS provider via a call to the MessageConsumer object's setMessageListener method.
Once initialized, the application is free to continue with its own processing. In the case of an actual e-mail client application, this may involve letting users compose new e-mail messages, organize existing e-mail into folders, and so on. The provider eventually calls back on the application's
MessageListener object when a new message arrives. This is the way JMS implements asynchronous messaging, which lets the client application perform other tasks while waiting for messages to arrive.
JMS also supports synchronous messaging, which requires the client application (or one of its child threads) to block until a message arrives. This type of an application is strictly an event-driven application, meaning it cannot do anything unless certain events occur; a JMS message arrival, in this case. This type of application can be developed to respond differently to different messages received, and can in effect be driven remotely by sending it the proper messages in the proper order.
setMessageListener method was called with a reference to an object that implements the
MessageListener interface. Because of this, once the JMS connection is started, the provider asynchronously calls the client application's
onMessage method when a message arrives for the destination the consumer is listening to; see the
EmailReader class's implementation of
onMessage (available electronically). The
javax.jms.Message object is provided as a parameter to the
onMessage method, representing the received JMS message. Once the message is cast to a
javax.jms.ObjectMessage object, the Email POJO is then extracted via a call to
getObject. For illustrative purposes, the remainder of the code simply prints out the e-mail's subject and body (see Figure 5).
When this application is executed, it receives all of the messages, one by one, that were placed on the queue earlier by the e-mail sender application. Once all of the messages have been received, the application simply waits to be notified that another message has been placed on the queue.
Writing JMS client applications is straightforward once you understand the basics. The power of JMS is in the ability to leverage built-in transaction management, and reliable message delivery, without knowing much more than the basics.
Eric is DDJ contributing editor and a consultant in New York. He has worked extensively in Java and C++ developing real-time trading and financial applications. He can be contacted at email@example.com.