
examples.jms.delayed-redelivery.readme.html Maven / Gradle / Ivy
HornetQ Delayed Redelivery Example Delayed Redelivery Example
This example demonstrates how HornetQ 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.
Example setup
Redelivery delay is specified in the configuration file hornetq-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
./build.sh
(orbuild.bat
on windows) from this directory
- Create an initial context to perform the JNDI lookup.
initialContext = getContext(0);
- Perform a lookup on the queue
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
- Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
- Create a JMS Connection
connection = cf.createConnection();
- Create a transacted JMS Session
Session session = connection.createSession(true, 0);
- Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
- Create a Text Message
TextMessage message = session.createTextMessage("this is a text message");
- Send the Message
producer.send(message);
- We commit the session to effectively send the message to the queue
session.commit();
- We create a JMS message consumer on the queue
MessageConsumer messageConsumer = session.createConsumer(queue);
- We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
connection.start();
- We receive the message...
TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000); System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
- ...but we roll back the session. the message returns to the queue, but only after a 5 second delay
session.rollback();
- We try to receive the message but it's being delayed
messageReceived = (TextMessage)messageConsumer.receive(3000); if (messageReceived != null) { return false; } System.out.println("Redelivery has been delayed so received message is " + messageReceived);
- We try and receive the message again, this time we should get it
messageReceived = (TextMessage)messageConsumer.receive(3000); System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
- We rollback the session again to cause another redelivery, and we time how long this one takes
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();
}
}
More information
- User Manual's Undelivered Messages chapter