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:
Create a temporary destination where the consumer can send replies.
In the message to be sent, set the JMSReplyTo field of the message header to that temporary 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.
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:
If you anticipate that the connection creating the temporary destination might terminate before the reply is sent.
If you need to send a persistent message to a temporary destination.