Configuring RabbitMQ Delayed Messages
Delayed messages enter queues for consumers to consume after a specified delay, instead of being received by consumers immediately after being sent.
Notes and Constraints
The delay exchange function is available only in RabbitMQ AMQP-0-9-1.
Configuring Delayed Messages
You can implement delayed messages in any of the following ways:
- Setting a delay exchange: Messages sent to a delay exchange carry the same delay time by default.
- Setting the message delay time: Each message can be set with different delay time.
- Setting the time to live and dead letter queue: Expired messages are automatically routed to the dead letter queue.
Messages sent to a delay exchange carry the same delay time by default.
The following example shows how to configure a delay exchange on a Java client:
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-delayed-type", "direct");
channel.exchangeDeclare("exchange", "x-delayed-message", true, false, args);
Each message can be set with different delay time.
The following example shows how to set the message delay time on a Java client.
final HashMap<String, Object> headers = new HashMap<>();
headers.put("x-delay", 10000);
final AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties().builder().headers(headers);
// publish a batch of messages to the main exchange
for (int i = 0; i < messageCount; i++) {
channel.basicPublish(exchangeName, routingKey, properties.build(),
("x-delayed-message-" + i).getBytes());
}
Expired messages are automatically routed to the dead letter queue.
The following example shows how to set the time to live and dead letter queue on a Java client:
channel.queueDeclare("dlq", false, false, false, null);
channel.exchangeDeclare("dlx", BuiltinExchangeType.DIRECT);
channel.queueBind("dlq", "dlx", "rt");
HashMap<String, Object> arguments = new HashMap<>();
arguments.put("x-dead-letter-exchange", "dlx");
arguments.put("x-dead-letter-routing-key", "rt");
arguments.put("x-message-ttl", 5000);
channel.exchangeDeclare("normal", BuiltinExchangeType.DIRECT);
channel.queueDeclare("normal", true, false, false, arguments);
channel.queueBind("normal", "normal", "key");
channel.basicPublish("normal", "key", null, "ttl".getBytes());
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot