Name
JMSExpiration — Purpose: Routing
Synopsis
A
Message
object can have an
expiration date, the same as on a carton
of milk. The expiration date is useful for messages that are only
relevant for a fixed amount of time. For example, the B2B example
developed in Chapter 4, and Chapter 5, might use expiration dates on messages
representing “Hot Deals” that a wholesaler extends to
retailers. The “Hot Deal” is only valid for a short time,
so the Message
that represents a deal expires
after that deadline.
The expiration time for messages is set in milliseconds by the
producer using the setTimeToLive(
)
method on either the QueueSender
or
TopicPublisher
as shown below:
// Publish-and-subscribe TopicPublisher topicPublisher = topicSession.createPublisher(topic); // Set time to live as 1 hour (1000 millis x 60 sec x 60 min)topicPublisher.setTimeToLive(3600000);
// Point-to-point QueueSender queueSender = queueSession.createSender(topic); // Set time to live as 2 days (1000 millis x 60 sec x 60 min x 48 hours)queueSender.setTimeToLive(172800000);
By default the timeToLive
is zero, which indicates
that the message doesn’t expire. Calling
setTimeToLive( )
with a zero value as the argument
ensures that message is created without an expiration date.
The JMSExpiration
date itself is calculated as:
JMSExpiration = currenttime + timeToLive.
The value of the currenttime
is the amount of
time, measured in milliseconds, that has elapsed since the Java epoch
(midnight, January 1, 1970, UTC).
The JMS ...
Get Java Message Service now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.