- RabbitMQ Cookbook
- Sigismondo Boschi Gabriele Santomaggio
- 324字
- 2021-07-19 18:52:49
Handling unroutable messages
In this example we are showing how to manage unroutable messages. An unroutable message is a message without a destination. For example, a message sent to an exchange without any bound queue.
Unroutable messages are not similar to dead letter messages; the first are messages sent to an exchange without any suitable queue destination. The latter, on the other hand, reach a queue but are rejected because of an explicit consumer decision, expired TTL, or exceeded queue length limit. You can find the source at Chapter01/Recipe13/Java_13/
.
Getting ready
To use this recipe you will need to set up the Java development environment as indicated in the Introduction section.
How to do it…
In order to handle unroutable messages, you need to perform the following steps:
- First of all we need to implement the class
ReturnListener
and its interface:public class HandlingReturnListener implements ReturnListener @Override public void handleReturn…
- Add the
HandlingReturnListener
class to the channelReturnListener
:channel.addReturnListener(new HandlingReturnListener());
- Then create an exchange:
channel.exchangeDeclare(myExchange, "direct", false, false, null);
- And finally publish a mandatory message to the exchange:
boolean isMandatory = true; channel.basicPublish(myExchange, "",isMandatory, null, message.getBytes());
How it works…
When we execute the publisher, the messages sent to myExchange
won't reach any destination since it has no bound queues. However, these messages aren't, they are redirected to an internal queue. The HandlingReturnListener
class will handle such messages using handleReturn()
.
The ReturnListener
class is bound to a publisher channel, and it will trap only its own unroutable messages.
You can also find a consumer in the source code example. Try also to execute the publisher and the consumer together, and then stop the consumer.
There's more…
If you don't set the channel ReturnListener
, the unroutable messages are silently dropped by the broker. In case you want to be notified about the unroutable messages, it's important to set the mandatory flag to true
; if false
, the unroutable messages are dropped as well.
- Java程序設計與開發
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- 精通軟件性能測試與LoadRunner實戰(第2版)
- concrete5 Cookbook
- 用戶體驗增長:數字化·智能化·綠色化
- Mastering JBoss Enterprise Application Platform 7
- Node.js全程實例
- Python Data Analysis Cookbook
- Mastering ROS for Robotics Programming
- HTML5 APP開發從入門到精通(微課精編版)
- Java網絡編程實戰
- Web性能實戰
- Python:Deeper Insights into Machine Learning
- Vue.js光速入門及企業項目開發實戰