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 sameeach 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.