-
Book Overview & Buying
-
Table Of Contents
RabbitMQ Cookbook
By :
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/.
To use this recipe you will need to set up the Java development environment as indicated in the Introduction section.
In order to handle unroutable messages, you need to perform the following steps:
ReturnListener and its interface:public class HandlingReturnListener implements ReturnListener @Override public void handleReturn…
HandlingReturnListener class to the channel ReturnListener:channel.addReturnListener(new HandlingReturnListener());
channel.exchangeDeclare(myExchange, "direct", false, false, null);
boolean isMandatory = true; channel.basicPublish(myExchange, "",isMandatory, null, message.getBytes());
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.
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.
Change the font size
Change margin width
Change background colour