2014-12-11 07:17:29 -05:00
|
|
|
# Message Expiry
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Messages can be set with an optional *time to live* when sending them.
|
|
|
|
|
2015-03-03 11:12:34 -05:00
|
|
|
Apache ActiveMQ will not deliver a message to a consumer after it's time to
|
2014-12-04 10:25:29 -05:00
|
|
|
live has been exceeded. If the message hasn't been delivered by the time
|
|
|
|
that time to live is reached the server can discard it.
|
|
|
|
|
2015-03-03 11:12:34 -05:00
|
|
|
Apache ActiveMQ's addresses can be assigned a expiry address so that, when
|
2014-12-04 10:25:29 -05:00
|
|
|
messages are expired, they are removed from the queue and sent to the
|
|
|
|
expiry address. Many different queues can be bound to an expiry address.
|
|
|
|
These *expired* messages can later be consumed for further inspection.
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Message Expiry
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-03-03 11:12:34 -05:00
|
|
|
Using Apache ActiveMQ Core API, you can set an expiration time directly on the
|
2014-12-04 10:25:29 -05:00
|
|
|
message:
|
|
|
|
|
|
|
|
// message will expire in 5000ms from now
|
|
|
|
message.setExpiration(System.currentTimeMillis() + 5000);
|
|
|
|
|
|
|
|
JMS MessageProducer allows to set a TimeToLive for the messages it sent:
|
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
// messages sent by this producer will be retained for 5s (5000ms) before expiration
|
2014-12-04 10:25:29 -05:00
|
|
|
producer.setTimeToLive(5000);
|
|
|
|
|
|
|
|
Expired messages which are consumed from an expiry address have the
|
|
|
|
following properties:
|
|
|
|
|
|
|
|
- `_HQ_ORIG_ADDRESS`
|
|
|
|
|
|
|
|
a String property containing the *original address* of the expired
|
|
|
|
message
|
|
|
|
|
|
|
|
- `_HQ_ORIG_QUEUE`
|
|
|
|
|
|
|
|
a String property containing the *original queue* of the expired
|
|
|
|
message
|
|
|
|
|
|
|
|
- `_HQ_ACTUAL_EXPIRY`
|
|
|
|
|
|
|
|
a Long property containing the *actual expiration time* of the
|
|
|
|
expired message
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Configuring Expiry Addresses
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Expiry address are defined in the address-setting configuration:
|
|
|
|
|
|
|
|
<!-- expired messages in exampleQueue will be sent to the expiry address expiryQueue -->
|
|
|
|
<address-setting match="jms.queue.exampleQueue">
|
|
|
|
<expiry-address>jms.queue.expiryQueue</expiry-address>
|
|
|
|
</address-setting>
|
|
|
|
|
|
|
|
If messages are expired and no expiry address is specified, messages are
|
|
|
|
simply removed from the queue and dropped. Address wildcards can be used
|
2015-03-13 18:37:28 -04:00
|
|
|
to configure expiry address for a set of addresses (see [Understanding the Wildcard Syntax](wildcard-syntax.md)).
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Configuring The Expiry Reaper Thread
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
A reaper thread will periodically inspect the queues to check if
|
|
|
|
messages have expired.
|
|
|
|
|
|
|
|
The reaper thread can be configured with the following properties in
|
|
|
|
`activemq-configuration.xml`
|
|
|
|
|
|
|
|
- `message-expiry-scan-period`
|
|
|
|
|
|
|
|
How often the queues will be scanned to detect expired messages (in
|
|
|
|
milliseconds, default is 30000ms, set to `-1` to disable the reaper
|
|
|
|
thread)
|
|
|
|
|
|
|
|
- `message-expiry-thread-priority`
|
|
|
|
|
|
|
|
The reaper thread priority (it must be between 0 and 9, 9 being the
|
|
|
|
highest priority, default is 3)
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Example
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
See the [examples.md](examples.md) chapter for an example which shows how message expiry is configured and used with JMS.
|