Search code examples
rabbitmqamqp

Is it possible with RabbitMQ to preserve direct exchange message without any queues present?


I wonder if the following scenario is possible:

  • Create an exchange of type direct
  • Publish a message to that exchange with routing key rk1
  • After that:
    • Create a queue which accepts messages with routing key rk1
    • Consume message published to exchange

It seems like if there is no queue present, the message is dropped and there is no way to receive it.

So basically I want to be able to produce messages when there are no consumers present. And consume them some time later.


Solution

  • It seems like if there is no queue present, the message is dropped and there is no way to receive it.

    Yes, this is correct, but it's only part of the story.

    A message queue is the storage location for messages published to the server. A consumer is a designated connection set to receive messages put into a queue. The exchange is simply a location to push messages. It contains the routing semantics to determine which messages wind up in the queues on the server. When a message cannot be routed to a queue and/or consumer, there are various semantics that can apply, but the default is that the message is dropped.

    Options for dealing with unroutable messages:

    • Alternate exchange - designates a different exchange where messages can be dumped if they cannot be routed to a queue on the current exchange. This can be thought of similar to how TCP/IP works when a destination host is not reachable on the current subnet, and traffic is forwarded to the gateway. Note that a queue must be bound to the alternate exchange for the message to be dumped into. A typical case might be to have it configured as fanout exchange with one queue to trap all messages sent into the alternate exchange.
    • Mandatory or Immediate - return a message back to the sender if it can't be delivered. The server does not store the message.
      • Mandatory designates that the message must be deliverable to a queue at the time it is published. If the message is not routable, the publisher will receive a basic.return.
      • Immediate designates that, in addition to being deliverable, must be immediately routed to a consumer on a particular queue (e.g. it's not good enough that it be dumped in a queue for pickup later - it has to be delivered to the end consumer right now.

    In every case, if there is no queue, the server cannot store the message.