JMS Expiration Example

To run the example, simply type mvn verify from this directory, 
or mvn -PnoServer verify if you want to start and create the server manually.

This example shows you how to configure ActiveMQ Artemis so messages are expipired after a certain time.

Messages can be retained in the messaging system for a limited period of time before being removed. JMS specification states that clients should not receive messages that have been expired (but it does not guarantee this will not happen).

ActiveMQ Artemis can assign a expiry address to a given queue so that when messages are expired, they are removed from the queue and routed to this address. These "expired" messages can later be consumed for further inspection.

The example will send 1 message with a short time-to-live to a queue. We will wait for the message to expire and checks that the message is no longer in the queue it was sent to. We will instead consume it from an expiry queue where it was moved when it expired.

Example setup

Expiry destinations are defined in the configuration file broker.xml:

         <address-setting match="jms.queue.exampleQueue">
            <expiry-address>jms.queue.expiryQueue</expiry-address>
         </address-setting>
         
     

This configuration will moved expired messages from the exampleQueue to the expiryQueue

ActiveMQ Artemis allows to specify either a Queue by prefixing the expiry-address with jms.queue. or a Topic by prefixing with jms.topic..
In this example, we will use a Queue to hold the expired messages.

Since we want to consume messages from this expiryQueue, we also need to add a JNDI binding to perform a lookup. This is configured in activemq-jms.xml

         <queue name="expiryQueue">
            <entry name="/queue/expiryQueue"/>
         </queue>