Dead Letter Example

This example shows you how to define and deal with dead letter messages.

Messages can be delivered unsuccessfully (e.g. if the transacted session used to consume them is rolled back). Such a message goes back to the JMS destination ready to be redelivered. However, this means it is possible for a message to be delivered again and again without any success and remain in the destination, clogging the system.

To prevent this, messaging systems define dead letter messages: after a specified unsuccessful delivery attempts, the message is removed from the destination and instead routed to a dead letter address where they can be consumed for further investigation.

The example will show how to configure ActiveMQ to route a message to a dead letter address after 3 unsuccessful delivery attempts.
The example will send 1 message to a queue. We will deliver the message 3 times and rollback the session every time.
On the 4th attempt, there won't be any message to consume: it will have been moved to a dead letter address.
We will then consume this dead letter message.

Example setup

Dead letter addresses and maximum delivery attempts are defined in the configuration file broker.xml:

         <address-setting match="jms.queue.exampleQueue">
            <dead-letter-address>jms.queue.deadLetterQueue</dead-letter-address>
            <max-delivery-attempts>3</max-delivery-attempts>
         </address-setting>
         
     

This configuration will moved dead letter messages from exampleQueue to the deadLetterQueue.

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

The maximum attempts of delivery is 3. Once this figure is reached, a message is considered a dead letter message and is moved to the deadLetterQueue.

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

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

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            InitialContext initialContext = getContext();
            
  3. We look up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We look up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We create a JMS transacted session
               Session session = connection.createSession(true, 0);
            
  10. We create a JMS message producer on the session. This will be used to send the messages
  11.           MessageProducer messageProducer = session.createProducer(topic);
           
  12. We create a text messages
  13.             TextMessage message = session.createTextMessage("this is a text message");
            
  14. We send the message to the queue
  15.             producer.send(message);
            
  16. We commit the session to effectively send the message to the queue
  17.             session.commit();
            

    We will now consume the message from the queue 3 times and roll back the session every time

  18. We create a JMS message consumer on the queue
  19.             MessageConsumer messageConsumer = session.createConsumer(queue);
            
  20. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  21.            connection.start();
            
  22. We receive the message a 1st time
  23.             TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
                System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());            
            
  24. We roll back the session. The message we received is undelivered and goes back to the queue
  25.             session.rollback();
            
  26. We receive a message and roll back the session a 2nd time
                messageReceived = (TextMessage)messageConsumer.receive(5000);
                System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
                session.rollback();
            
  27. We do it againt a 3rd time
               messageReceived = (TextMessage)messageConsumer.receive(5000);
               System.out.println("3rd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
               session.rollback();
           

    Since the queue was configured to move messages to the deadLetterQueue after 3 unsuccessful delivery attempts, the message won't be in the queue anymore

  28. We try to receive a message from the queue for a 4th. Since there is none, the call will timeout after 5000ms and messageReceived will be null
               messageReceived = (TextMessage)messageConsumer.receive(5000);
               System.out.println("4th delivery from " + queue.getQueueName() + ": " + messageReceived);
            

    We have configured ActiveMQ to send any dead letter messages to the deadLetterQueue. We will now consume messages from this queue and receives the dead letter messages.

  29. We look up the JMS dead letter queue object from JNDI
  30.            Queue deadLetterQueue = (Queue)initialContext.lookup("/queue/deadLetterQueue");
            
  31. We create a JMS message consumer on the dead letter queue
  32.             MessageConsumer deadLetterConsumer = session.createConsumer(expiryQueue);
            
  33. We consume a message from the dead letter queue:
  34.             messageReceived = (TextMessage)deadLetterConsumer.receive(5000);
            
  35. The message consumed from the dead letter queue has the same content than the message which was sent to the queue
                System.out.println("Received message from " + deadLetterQueue.getQueueName() + ": " + messageReceived.getText());
            

    JMS does not specify the notion of dead letter destinations and messages. From JMS point of view, the message received from the dead letter queue is a different message than the message removed from the queue after the unsuccessful delivery attempts: the messages have the same content (properties and body) but their JMS headers differ.
    ActiveMQ defines additional properties for messages received from a dead letter destination

  36. The message's destination is the dead letter queue
  37.             System.out.println("Destination of the message: " + ((Queue)messageReceived.getJMSDestination()).getQueueName());
            
  38. The origin destination is stored in the _AMQ_ORIG_ADDRESS property
                System.out.println("*Origin destination* of the message: " + messageReceived.getStringProperty("_AMQ_ORIG_ADDRESS"));
            
  39. We do not forget to commit the session to acknowledge that we have received the message from the dead letter queue
  40.             session.commit();
            

  41. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  42.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }
            

More information