Return-Path:
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.
@@ -44,7 +45,7 @@ under the License.
<expiry-address>jms.queue.expiryQueue</expiry-address>
</address-setting>
-
+
This configuration will moved expired messages from the ActiveMQ Artemis allows to specify either a To run the example, simply type We will now try to consume the message from the queue but it won't be there since it has expired However, we have configured ActiveMQ Artemis to send any expired messages to the JMS does not specify the notion of expiry queue. From JMS point of view, the message received from the expiry queue
- is a different message than the message expired from the queue: the two messages have the same content (properties and body) but
- their JMS headers differ. As we have not defined a time-to-live for the expiry queue, messages sent to the expiry queue will be kept forever (their JMS Expiration value is 0)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.exampleQueue
to the expiryQueue
Queue
by prefixing the expiry-address
with jms.queue.
or a Topic
by prefixing with jms.topic.
.
@@ -56,151 +57,5 @@ under the License.
<entry name="/queue/expiryQueue"/>
</queue>
- Example step-by-step
- mvn verify -Pexample
from this directory
-
client-jndi.properties
file in the directory ../common/config
-
-
- InitialContext initialContext = getContext();
-
-
-
- Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
-
-
-
- ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
-
-
-
- connection = cf.createConnection();
-
-
-
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
-
-
- MessageProducer messageProducer = session.createProducer(topic);
-
-
-
- producer.setTimeToLive(1000);
-
-
-
- TextMessage message = session.createTextMessage("this is a text message");
-
-
-
- producer.send(message);
-
-
-
- Thread.sleep(5000);
-
-
-
- MessageConsumer messageConsumer = session.createConsumer(queue);
-
-
-
- connection.start();
- messageReceived
will be null
-
-
-
- TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
- System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);
- expiryQueue
.
- We will now consume messages from this expiry queue and receives the expired message.
-
-
- Queue expiryQueue = (Queue)initialContext.lookup("/queue/expiryQueue");
-
-
-
- MessageConsumer expiryConsumer = session.createConsumer(expiryQueue);
-
-
-
- messageReceived = (TextMessage)expiryConsumer.receive(5000);
-
-
-
- System.out.println("Received message from " + expiryQueue.getQueueName() + ": " + messageReceived.getText());
-
- ActiveMQ Artemis defines additional properties to correlate the message received from the expiry queue with the
- message expired from the queue
-
-
- System.out.println("Destination of the expired message: " + ((Queue)messageReceived.getJMSDestination()).getQueueName());
-
-
-
- System.out.println("Expiration time of the expired message (relative to the expiry queue): " + messageReceived.getJMSExpiration());
- _HORNETQ_ORIG_DESTINATION
property
-
-
-
- System.out.println("*Origin destination* of the expired message: " + messageReceived.getStringProperty("_HORNETQ_ORIG_DESTINATION"));
- _HORNETQ_ACTUAL_EXPIRY
property
-
-
-
- System.out.println("*Actual expiration time* of the expired message: " + messageReceived.getLongProperty("_HORNETQ_ACTUAL_EXPIRY"));
-
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
- finally
- {
- if (initialContext != null)
- {
- initialContext.close();
- }
- if (connection != null)
- {
- connection.close();
- }
- }
-
-
-
-