23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

JMSReplyTo Purpose: Routing<br />

In some cases, a message producer may want the consumers to reply to a message. The<br />

JMSReplyTo header indicates which address, if any, a JMS consumer should reply to. The<br />

JMSReplyTo header is set explicitly by the JMS client; its contents will be a<br />

javax.jms.Destination object (either Topic or Queue).<br />

In some cases the JMS client will want the message consumers to reply to a temporary<br />

topic or queue set up by the JMS client. Here is an example of a pub/sub JMS client that<br />

creates a temporary topic and uses its Topic object identifier as a JMSReplyTo header:<br />

TopicSession session =<br />

connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);<br />

...<br />

Topic tempTopic = session.createTemporaryTopic( );<br />

...<br />

Text<strong>Message</strong> message = session.createText<strong>Message</strong>( );<br />

message.setText(text);<br />

message.setJMSReplyTo(tempTopic);<br />

publisher.publish(message);<br />

When a JMS message consumer receives a message that includes a JMSReplyTo destination,<br />

it can reply using that destination. A JMS consumer is not required to send a reply, but in<br />

some JMS applications clients are programmed to do so. Here is an example of a JMS<br />

consumer that uses the JMSReplyTo header on a received message to send a reply. In this<br />

case, the reply is a simple empty <strong>Message</strong> object:<br />

Topic chatTopic = ... get topic from somewhere<br />

...<br />

// Publisher is created without a specified Topic<br />

TopicPublisher publisher = session.createPublisher(null);<br />

...<br />

public void on<strong>Message</strong>(<strong>Message</strong> message){<br />

try {<br />

Text<strong>Message</strong> text<strong>Message</strong> = (Text<strong>Message</strong>)message;<br />

Topic replyTopic = (Topic)text<strong>Message</strong>.getJMSReplyTo( );<br />

<strong>Message</strong> reply<strong>Message</strong> = session.create<strong>Message</strong>( );<br />

publisher.publish(replyTopic, reply<strong>Message</strong>);<br />

} catch (JMSException jmse){jmse.printStackTrace( );}<br />

}<br />

The JMSReplyTo destination set by the message producer can be any destination in the<br />

messaging system. Using other established topics or queues allows the message producer<br />

to express routing preferences for the message itself or for replies to that message.<br />

Typically, this kind of routing is used in workflow applications. In a workflow application,<br />

a message represents some task that is processed one step at a time by several JMS clients<br />

- possibly over days. For example, an order message might be processed by sales first, then<br />

inventory, then shipping, and finally accounts receivable. When each JMS client (sales,<br />

inventory, shipping, or accounts receivable) is finished processing the order data, it could<br />

use the JMSReplyTo address to deliver the message to the next step.<br />

164

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!