4

Using the RabbitMQ web UI, When I publish to a topic exchange with no current queue bindings, rabbitmq is saying that the message has been published but not routed.

Using amqp.node, when I bind a queue to the exchange and start consuming using "#" (all), I am not getting anything.

I am expecting to get the previously published but unrouted message. Is this possible?

Kyle Domingo
  • 521
  • 4
  • 14

3 Answers3

5

I am expecting to get the previously published but unrouted message. Is this possible?

this is not directly possible.

if a message is not routed anywhere, it will disappear into the void. if you need to hang on to it, you must route it to a queue. there is no way to get a message that was previously sent nowhere, and re-route it.

your options for workarounds are very limited. you must always ensure a message is routed to a queue.

this can be done if you use a alternate exchange (https://www.rabbitmq.com/ae.html) to route messages that go nowhere, through a different exchange.

when a message goes nowhere through the first exchange, it will then go through the alternate exchange. at that point, you must ensure the message goes to a queue.

later, when you need to process the message that went through the alternate exchange, you will have to read the message from the queue that it is currently in and re-publish it through the correct exchange.

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
0

Handle unrouted messages it is possible by using the mandatory flag, and add the ReturnListener in this way:

    final Connection connection = factory.newConnection();
            final Channel channel = connection.createChannel();

            channel.addReturnListener(new ReturnListener() {
                public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {

                    System.out.println("unrouted  messages here!!!");

                }
            });
     bool isMandatory = true;
      channel.basicPublish("my_exchange","not_routed",isMandatory,null,"not_routed_message".getBytes());

I don't know if the php library supports this listener, but the standard client can do that.

that it is what the UI does to show the message:

Message published, but not routed.

check this: https://github.com/rabbitmq/rabbitmq-management/blob/0f1013c57a7341a7fb0afc73d5b6b63fd9275c6d/src/rabbit_mgmt_wm_exchange_publish.erl#L73

Gabriele Santomaggio
  • 21,656
  • 4
  • 52
  • 52
  • 1
    that's an interesting idea! in this case, the message would be delivered back to the producer and you could handle the storage and re-publishing later, right? could batch up the undelivered messages and have some kind of timer to re-try publishing them. i'll have to keep this one in mind. :) – Derick Bailey Aug 17 '16 at 14:04
  • 1
    Yes, you will have back your message(s), and you can decide to re-publish it later, store etc. – Gabriele Santomaggio Aug 17 '16 at 14:11
0

You are looking for this: https://www.rabbitmq.com/ae.html

Specify an alternative exchange if the message was not routed.

Daniel
  • 795
  • 1
  • 10
  • 25