Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In some cases, a message producer may want the consumers to reply to a message. The JMSReplyTo header indicates which destination, if any, a JMS consumer should reply to. The JMSReplyTo header is set explicitly by the JMS client; its contents will be a javax.jms.Destination object (either Topic or Queue).
In some cases, the JMS client will want the message consumers to reply to a temporary topic or queue set up by the JMS client. Here is an example of a pub/sub JMS client that creates a temporary topic and uses its Topic object identifier as a JMSReplyTo header:
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); ... Topic tempTopic = session.createTemporaryTopic(); ... TextMessage message = session.createTextMessage(); message.setText(text); message.setJMSReplyTo(tempTopic); publisher.publish(message);
When a JMS message consumer receives a message that includes a JMSReplyTo destination, it can reply using that destination. A JMS consumer is not required to send a reply, but in some JMS applications, clients are programmed to do so. Here is an example of a JMS consumer that uses the JMSReplyTo header on a received message to send a reply. In this case, the subscriber will simple send an acknowledgment back to the publisher indicating it received the message:
Topic chatTopic = ... get topic from somewhere
...
// Publisher is created without a specified Topic
TopicPublisher publisher = session.createPublisher(null);
...
public void onMessage(Message message){
try {
TextMessage textMessage = (TextMessage)message;
Topic replyTopic = (Topic)textMessage.getJMSReplyTo();
TextMessage replyMessage = session.createTextMessage("Received Message...");
publisher.publish(replyTopic, replyMessage);
} catch (JMSException jmse){jmse.printStackTrace();}
}
The JMSReplyTo destination set by the message producer can be any destination in the messaging system. Using other established topics or queues allows the message producer to express routing preferences for the message itself or for replies to that message. Typically, this kind of routing is used in workflow applications. In a workflow application, a message represents some task that is processed one step at a time by several JMS clients—possibly over days. For example, an order message might be processed by sales first, then inventory, then shipping, and finally accounts receivable. When each JMS client (sales, inventory, shipping, or accounts receivable) is finished processing the order data, it could use the JMSReplyTo address to deliver the message to the next step.