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 />

publish( ) method returns, and allows client execution to continue. More on how and why<br />

this works reliably can be found in Chapter 6.<br />

4.4.1 Persistent <strong>Message</strong>s and Temporary Topics<br />

When you are using a temporary topic as a way of posting a reply to a message, you<br />

should realize that the total round trip (the initial message and the reply) isn't guaranteed to<br />

survive a certain failure condition, even if you use persistent messages. The problem is that<br />

temporary topics cannot be used for durable subscriptions. Consider the following<br />

scenario:<br />

1. A JMS client (producer) creates a temporary topic, puts it in the JMSReplyTo header<br />

of a message, marks the message as persistent, and publishes it.<br />

2. The subscriber gets the message and publishes a response on the temporary topic<br />

using a persistent message.<br />

3. The original producer expects a reply on the temporary topic, but disconnects or<br />

crashes before it is received.<br />

4. The original producer restarts, and is no longer able to subscribe to the original<br />

temporary topic that it had established in its previous life. It can't resubscribe<br />

because the temporary topic was only valid for the duration of the previous<br />

connection. Calling createTemporaryTopic( ) in the new session returns a new<br />

temporary topic, not the previous one.<br />

This is a subtle point, since any client with a nondurable subscription will not get messages<br />

during a failure. In other scenarios it may be acceptable to lose messages for a time, yet<br />

still be able to start receiving newly published "responses" when the original producer of<br />

the message starts up again. In the B2B example, a failure of the Wholesaler means that<br />

the reply messages sent to the temporary topic will be lost. An alternative and superior<br />

design would use the JMSReplyTo header, with an established topic instead of a temporary<br />

one. Chapter 6 provides more detail on message delivery semantics, Quality of <strong>Service</strong>,<br />

and failure conditions.<br />

4.5 JMSCorrelationID<br />

In the B2B example, we are using the JMSCorrelationID as a way for the Retailer to<br />

associate its identity with its reply message, as illustrated by the following code in<br />

Retailer.autoBuy( ) :<br />

private void autoBuy (javax.jms.<strong>Message</strong> message){<br />

...<br />

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

textMsg.setJMSCorrelationID("DurableRetailer");<br />

}<br />

publisher.publish(<br />

textMsg,<br />

javax.jms.DeliveryMode.PERSISTENT,<br />

javax.jms.<strong>Message</strong>.DEFAULT_PRIORITY,<br />

1800000);<br />

...<br />

64

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

Saved successfully!

Ooh no, something went wrong!