官术网_书友最值得收藏!

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:

  1. First of all we need to implement the class ReturnListener and its interface:
    public class HandlingReturnListener implements ReturnListener
    @Override
      public void handleReturn…
  2. Add the HandlingReturnListener class to the channel ReturnListener:
    channel.addReturnListener(new HandlingReturnListener());
  3. Then create an exchange:
    channel.exchangeDeclare(myExchange, "direct", false, false, null);
  4. 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.

主站蜘蛛池模板: 大洼县| 伊金霍洛旗| 荔浦县| 安岳县| 河西区| 花莲县| 繁峙县| 营山县| 灵山县| 法库县| 锦屏县| 扶沟县| 兴和县| 宁强县| 林州市| 滨海县| 昂仁县| 西乌珠穆沁旗| 高州市| 漳平市| 营口市| 昌乐县| 和政县| 利川市| 巴南区| 伊金霍洛旗| 定安县| 阿拉善左旗| 泰宁县| 尤溪县| 四川省| 麻栗坡县| 恭城| 禹州市| 桃源县| 全南县| 上高县| 高唐县| 乐昌市| 赤壁市| 梁山县|