Channels ▼

JVM Languages

Java Message Service

Source Code Accompanies This Article. Download It Now.

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.

Again, the 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 protected]

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.