Message Expiration
When a messaging system provides durable storage of messages
the messages are potentially able to have a very long lifetime. In some
scenarios this is useful as some messages may be critical to business operations
and should be preserved in scenarios where a receiving application may
experience a prolonged failure or is disconnected for a period of time. In
other scenarios messages may only be relevant for a specific time period. Weather
reports or current stock prices will be useful when they have recently been
created, but after a few days, or even hours they will become less relevant.
Providing an expiration mechanism for messages allows them
to be removed from the messaging system when they reach a point in time when
they cease to be relevant to the receiving application. Such a mechanism can
also be used to deadletter messages that should have been processed by a
specified point in time, and action needs to be taken.
Service Bus brokered messaging provides support for message
expiration. Queues, topics and subscriptions have a DefaultMessageTimeToLive
property that can be set on creation to specify the default time that a message
can remain in the messaging system before it expires. The expiration time for
individual messages can be specified by the sending application by setting the ExpiresAtUtc
property of the message before enqueueing it.
Setting
DefaultMessageTimeToLive
When creating a messaging entity the default time for
message expiration can be specified in the entity description class when the
entity is created. The following code will set the DefaultMessageTimeToLive
property of the pizzaorders queue to 60 minutes. Any messages that are places
on the queue that do not have a specified time to live will expire within 60
minutes.
QueueDescription pizzaOrdersDescription = new
QueueDescription("pizzaorders")
{
DefaultMessageTimeToLive = TimeSpan.FromMinutes(60)
};
namespaceMgr.CreateQueue(pizzaOrdersDescription);
|
Remember that once a messaging entity is created the
properties cannot be changed, so care must be taken when specifying this
property in production systems.
Setting
Time to Live on Individual Messages
In Service Bus brokered messaging the BrokeredMessage class
has two properties that are related to message expiration.
·
TimeToLive – A TimeSpan property that represents the time that a
message can spend in the messaging system before it expires.
·
ExpiresAtUtc – A read only DateTime property that specifies the
UTC time at which a message will expire.
When setting the time for a message to expire the TimeToLive
property must be used to specify the time duration that a message can spend on
the queue or subscription before it expires. When the message is enqueued, the EnqueuedTimeUtc
property of the message will be set to the current UTC time on the server and
the ExpiresAtUtc property will be set to the current UTC time on the server
plus the value of the TimeToLive property.
The following code shows how the TimeToLive property of a message
can be set to 30 minutes.
BrokeredMessage orderMsg = new BrokeredMessage(order);
orderMsg.TimeToLive
= TimeSpan.FromMinutes(30);
|
It should be noted that the ExpiresAtUtc property will be
set when the message is enqueued, not when the TimeToLive property is set.