Sun Java System Message Queue 3.7 UR1 Technical Overview

The Request-Reply Pattern

You can combine producers and consumers in the same connection (or even session when using the unified API). In addition, the JMS API allows you to implement a request-reply pattern for your messaging operations by using temporary destinations.

To set up the request-reply pattern you need to do the following:

  1. Create a temporary destination where the consumer can send replies.

  2. In the message to be sent, set the JMSReplyTo field of the message header to that temporary destination.

When the message consumer processes the message, it examines the JMSReplyTo field of the message to determine if a reply is required and sends the reply to the specified destination.

The request-reply mechanism saves the producer the trouble of setting up an administered object for the reply destination and makes it easy for the consumer to respond to the request. This pattern is useful when the producer must be sure that a request has been handled before proceeding.

Figure 2–6 illustrates a request-reply pattern that sends messages to a topic and receives replies in a temporary queue.

Figure 2–6 Request/Reply Pattern

A publisher sends a message to two subscribers via a
topic destination and receives replies via a queue. Figure is explained in
text.

As the figure shows, MyTopicPublisher produces Msg1 to the destination MyTopic. MyTopicSubsriber1 and MyTopicSubscriber2 receive the message and send a reply to MyTempQueue, from where MyTQReceiver retrieves it. This pattern might be useful for an application that published pricing information to a large number of clients and which queued their (reply) orders for sequential processing.

Temporary destinations last only as long as the connection that created them. Any producer can send to a temporary destination, but the only consumers that can access temporary destinations are those created by the same connection that created the destination.

Since the request/reply pattern depends on creating temporary destinations, you should not use this pattern in the following cases: