Channels ▼


Java Message Service

Source Code Accompanies This Article. Download It Now.

JMS Message Paradigms

JMS supports two main message paradigms:

  • Point-to-point (or queue-based) messaging.
  • Publish-and-subscribe (or topic-based) messaging.

JMS can support the messaging concept of request-and-reply through both of these messaging domains.

Request-and-reply messaging is a common form of client-server communication, where a client makes a request to a server, and the server sends back a response. One of the most familiar implementations of this paradigm is the communication between a web browser and web server; see Figure 1. In this exchange, the client sends a request to the server, and the server responds with the requested data.

Figure 1: Request-and-reply messaging.

Queue-Based Messaging

The JMS point-to-point domain, otherwise known as queue-based messaging or store-and-forward, is normally used when offline message processing is required. The classic example is an e-mail system. If you're currently logged on and an e-mail arrives, you see it immediately and can read it. If you shut down your system, your e-mail is safely and reliably stored for you to view at a later time (see Figure 2).

Figure 2: E-mail inbox implemented as a store-and-forward queue.

When the e-mail reader application is not running, sent e-mail messages are safely stored. Once the e-mail reader application is started, the e-mail messages are delivered.

An e-mail inbox is basically a queue. When users start the e-mail reader application, the messages that were safely stored in the queue are delivered. The e-mail application's display should indicate that new e-mail messages are present, and users can view them (see Figure 3). Once delivered, the message is removed from the "Inbox" queue. It may be placed on another queue that holds messages that have been read, or it may be discarded altogether. That decision is application specific, although the mechanics of the queue are the same for all applications.

Figure 3: When the e-mail reader starts, messages are delivered from the queue to the reader application.

In JMS, a queue is represented by the javax.jms.Queue interface, which extends the Destination interface, and it's either defined by an administrator or the client application at runtime. One or more message producers can place messages onto the same queue, and one or more message consumers can listen to a queue to receive messages. However, each message is delivered to only one consumer. Having multiple consumers listen to the same queue helps balance the load of message processing, or ensures that if one consumer fails, there is another there to continue processing messages off of the queue. Regardless, the behavior is the same—each message on the queue is processed exactly once.

A queue has the added benefit that it stores messages even when there are no listeners available. This feature combined with the queue's exactly-once processing behavior makes the JMS queue the usual choice for reliable, point-to-point messaging in distributed software systems.

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.