Dead Letter
The reason why a message is called dead letter message requires that:
The message is rejected by the consumer (via basic.reject or back.nack) with requeue=false.
The message expired.
The queue is set with a TTL (Time To Live) time and the message expires.
The message that exceeds the length limit of the queue is discarded.
Configure the dead letter exchange for the queue and specify the "x-dead-letter-exchange" and "x-dead-letter-routing-key" parameters when declaring the queue. The queue sends the dead letter message to the dead letter exchange according to "x-dead-letter-exchange" and sets the dead letter routing key for the dead letter message according to "x-dead-letter-routing-key".
The following example demonstrates the configuration of a dead letter exchange and routing in the Java client
channel.exchangeDeclare("some.exchange.name", "direct");
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-dead-letter-exchange", "some.exchange.name");
args.put("x-dead-letter-routing-key", "some-routing-key");
channel.queueDeclare("myqueue", false, false, false, args);
TTL
RabbitMQ can set TTL for messages and queues, currently, there are two methods to set it. The first method is to set it through queue properties, where all messages in the queue have the same expiration time. The second is to set the messages separately, and the TTL of each message can be different. If both methods are used, the expiration time of the message is based on the smaller TTL between the two. Once the survival time of a message in the queue exceeds the set TTL value, it is called a dead message, and the consumer will no longer be able to receive the message.
Set up queue TTL
The way to set the message TTL through the queue properties is to add the x-message-ttl parameter to the queue.declare method, the unit is ms.
The following example demonstrates setting up a queue TTL in the Java client.
Map<String, Object> argss = new HashMap<String, Object>();
argss.put("x-message-ttl",6000);
channel.queueDeclare(queueName, durable, exclusive, autoDelete, argss);
Set up message TTL
The way to set the TTL for each message is to add the expiration property parameter in the basic.publish method, the unit is ms.
The following example demonstrates setting up a message TTL through queue properties in the Java client.
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
builder.deliveryMode(2);
builder.expiration("6000");
AMQP.BasicProperties properties = builder.build();
channel.basicPublish(exchangeName,routingKey,mandatory,properties,"ttlTestMessage".getBytes());