- RabbitMQ Cookbook
- Sigismondo Boschi Gabriele Santomaggio
- 579字
- 2021-07-19 18:52:50
Managing rejected or expired messages
In this example, we show how to manage expired or rejected messages using dead letter exchanges. The dead letter exchange is a normal exchange where dead messages are redirected; if not specified, dead messages are just dropped by the broker.
You can find the source in Chapter02/Recipe04/Java/src/rmqexample
, where you can find the following files:
Producer.java
Consumer.java
To try expired messages, you can use the first code alone that publishes messages with a given TTL, as shown in the How to let messages expire on specific queues recipe.
Once started, the consumer of the example will not allow the messages to expire but will reject all the messages, leading to dead messages as well.
Getting ready
To use this recipe, we need to set up the Java development environment, as indicated in the Introduction section of Chapter 1, Working with AMQP.
How to do it...
Complete the following steps to show how to manage expired or rejected messages using dead letter exchanges:
- We create the work exchange and the dead letter exchange:
channel.exchangeDeclare(Constants.exchange, "direct", false); channel.exchangeDeclare(Constants.exchange_dead_letter, "direct", false);
- We create the queue using the
dead_letter exchange
andx-message-ttl
arguments:arguments.put("x-message-ttl", 10000); arguments.put("x-dead-letter-exchange",exchange_dead_letter); channel.queueDeclare(queue, false, false, false, arguments);
- Then, we bind
queue
as follows:channel.queueBind(queue, exchange, "");
- We can finally send the messages to
exchange
usingchannel.basicPublish()
. - To try the rejected messages, we have to configure a consumer, as we have seen in the previous examples, and reject the message using the following code:
basicReject(envelope.getDeliveryTag(), false);
How it works...
Let's start with the first scenario (using the producer alone): the expired messages. In step 1, we create two exchanges, the working exchange and the dead letter exchange. In step 2, we create the queue with the following two optional parameters:
- Message TTL using
arguments.put("x-message-ttl", 10000)
, as seen in the How to let messages expire on specific queues recipe. - The dead letter exchange name using
arguments.put("x-dead-letter-exchange", exchange_dead_letter);
As you can see, we simply add the configuration to the optional queue arguments. So, when the producer sends a message to exchange
, it will be routed to the queue
parameter. The message will expire after 10 seconds, after which it will be redirected to exchange_dead_letter
.
For the second scenario, the consumer of this recipe will reject messages. When this consumer gets a message, it sends back a negative acknowledgement (nack) using basicReject()
and when the broker receives the nack, it redirects the message to exchange_dead_letter
. By binding a queue to the dead letter exchange, you can manage these messages.
When a message is redirected to a dead letter queue, the broker modifies the header message and adds the following values in the x-dead
key:
reason
: This denotes whether the queue is expired or rejected (withrequeue = false
)queue
: This denotes the queue source, for example,stat_queue_02/05
time
: This denotes the date and time the message was dead letteredexchange
: This denotes the exchange source, for example,monitor_exchange_02/05
routing-keys
: This denotes the original routing keys used to send the message
To see these values in action, you can use the GetOneDeadLetterQ
class. This class creates a queue_dead_letter
queue and binds it to exchange_dead_letter
.
There's more...
You can also specify the dead letter routing key using arguments.put("x-dead-letter-routing-key", "myroutingkey")
. This parameter replaces the original routing key. This means that you can redirect different messages with different routing keys to the same queue. That's great!
- OpenStack Cloud Computing Cookbook(Third Edition)
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Apache Spark 2.x Machine Learning Cookbook
- Functional Programming in JavaScript
- 假如C語言是我發明的:講給孩子聽的大師編程課
- Learning Neo4j 3.x(Second Edition)
- Java Web開發技術教程
- STM32F0實戰:基于HAL庫開發
- 軟件項目管理實用教程
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- Python High Performance Programming
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Creating Data Stories with Tableau Public
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Python青少年趣味編程