- 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!
- Learning Cython Programming(Second Edition)
- ASP.NET Core 5.0開發入門與實戰
- ASP.NET Core Essentials
- Python零基礎快樂學習之旅(K12實戰訓練)
- Access 2010數據庫基礎與應用項目式教程(第3版)
- TypeScript項目開發實戰
- PLC編程與調試技術(松下系列)
- 碼上行動:用ChatGPT學會Python編程
- Create React App 2 Quick Start Guide
- 匯編語言編程基礎:基于LoongArch
- Learning Material Design
- 基于GPU加速的計算機視覺編程:使用OpenCV和CUDA實時處理復雜圖像數據
- Head First Kotlin程序設計
- 用Go語言自制編譯器
- JSP應用與開發技術(第3版)