This example demonstrates how ActiveMQ can be configured to provide a delayed redelivery in the case where a message needs to be redelivered.
Delaying redelivery can often be useful in the case that clients regularly fail or roll-back. Without a delayed redelivery, the system can get into a "thrashing" state, with delivery being attempted, the client rolling back, and delivery being re-attempted ad infinitum in quick succession, using up valuable CPU and network resources.
Re-delivery occurs when the session is closed with unacknowledged messages. The unacknowledged messages will be redelivered.
By providing a redelivery delay, it can be specified that a delay of, say, 10 seconds is implemented between rollback and redelivery. The specific delay is configurable on both a global and per destination level, by using wild-card matching on the address settings.
Redelivery delay is specified in the configuration file activemq-configuration.xml:
In this example we set the redelivery delay to 5 seconds for the specific example queue. We could set redelivery delay on
on multiple queues by specifying a wild-card in the match, e.g. match="jms.#"
would apply the settings
to all JMS queues and topics.
We then consume a message in a transacted session, and rollback, and note that the message is not redelivered until after 5 seconds.
<address-setting match="jms.queue.exampleQueue">
<redelivery-delay>5000</redelivery-delay>
</address-setting>
To run the example, simply type mvn verify -Pexample
from this directory
initialContext = getContext(0);
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(true, 0);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("this is a text message");
producer.send(message);
session.commit();
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
session.rollback();
messageReceived = (TextMessage)messageConsumer.receive(3000);
if (messageReceived != null)
{
return false;
}
System.out.println("Redelivery has been delayed so received message is " + messageReceived);
messageReceived = (TextMessage)messageConsumer.receive(3000);
System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
long start = System.currentTimeMillis();
session.rollback();
messageReceived = (TextMessage)messageConsumer.receive(8000);
long end = System.currentTimeMillis();
System.out.println("3nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText() +
" after " + (end - start) + " milliseconds.");
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();
}
}