23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

JMSExpiration Purpose: Routing<br />

A <strong>Message</strong> object can have an expiration date, the same as on a carton of milk. The<br />

expiration date is useful for messages that are only relevant for a fixed amount of time. For<br />

example, the B2B example developed in Chapter 4, and Chapter 5, might use expiration<br />

dates on messages representing "Hot Deals" that a wholesaler extends to retailers. The<br />

"Hot Deal" is only valid for a short time, so the <strong>Message</strong> that represents a deal expires after<br />

that deadline.<br />

The expiration time for messages is set in milliseconds by the producer using the<br />

setTimeToLive( ) method on either the QueueSender or TopicPublisher as shown below:<br />

// Publish-and-subscribe<br />

TopicPublisher topicPublisher = topicSession.createPublisher(topic);<br />

// Set time to live as 1 hour (1000 millis x 60 sec x 60 min)<br />

topicPublisher.setTimeToLive(3600000);<br />

// Point-to-point<br />

QueueSender queueSender = queueSession.createSender(topic);<br />

// Set time to live as 2 days (1000 millis x 60 sec x 60 min x 48 hours)<br />

queueSender.setTimeToLive(172800000);<br />

By default the timeToLive is zero, which indicates that the message doesn't expire. Calling<br />

setTimeToLive( ) with a zero value as the argument ensures that message is created<br />

without an expiration date.<br />

The JMSExpiration date itself is calculated as:<br />

JMSExpiration = currenttime + timeToLive.<br />

The value of the currenttime is the amount of time, measured in milliseconds, that has<br />

elapsed since the <strong>Java</strong> epoch (midnight, January 1, 1970, UTC).<br />

The JMS specification doesn't state whether the current time is calculated by the client<br />

computer or the message server, so consistency is dependent on either the accuracy of<br />

every client machine or the message server. We can certainly empathize with the JMS spec<br />

producers for remaining agnostic on this issue. Whether or not timestamps are<br />

synchronized across clients depends on the application. There is nothing preventing a JMS<br />

vendor from providing a configuration setting to control this behavior.<br />

The JMSExpiration is the date and time that the message will expire. JMS clients should be<br />

written to discard any unprocessed messages that have expired, because the data and event<br />

communicated by the message is no longer valid. <strong>Message</strong> providers (servers) are also<br />

expected to discard any undelivered messages that expire while in their queues and topics.<br />

Even persistent messages are supposed to be discarded if they expire before being<br />

delivered.<br />

161

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

Saved successfully!

Ooh no, something went wrong!