RabbitMQ: dropping messages when no consumers are connected

I'm trying to setup RabbitMQ in a model where there is only one producer and one consumer, and where messages sent by the producer are delivered to the consumer only if the consumer is connected, but dropped if the consumer is not present.

Basically I want the queue to drop all the messages it receives when no consumer is connected to it.

An additional constraint is that the queue must be declared on the RabbitMQ server side, and must not be explicitly created by the consumer or the producer.

Is that possible?

I've looked at a few things, but I can't seem to make it work:

  • durable vs non-durable does not work, because it is only useful when the broker restarts. I need the same effect but on a connection.
  • setting auto_delete to true on the queue means that my client can never connect to this queue again.
  • x-message-ttl and max-length make it possible to lose message even when there is a consumer connected.
  • I've looked at topic exchanges, but as far as I can tell, these only affect the routing of messages between the exchange and the queue based on the message content, and can't take into account whether or not a queue has connected consumers.
  • The effect that I'm looking for would be something like auto_delete on disconnect, and auto_create on connect. Is there a mechanism in rabbitmq that lets me do that?


    After a bit more research, I discovered that one of the assumptions in my question regarding x-message-ttl was wrong. I overlooked a single sentence from the RabbitMQ documentation:

    Setting the TTL to 0 causes messages to be expired upon reaching a queue unless they can be delivered to a consumer immediately

    https://www.rabbitmq.com/ttl.html

    It turns out that the simplest solution is to set x-message-ttl to 0 on my queue.


    You can not doing it directly, but there is a mechanism not dificult to implement.

    You have to enable the Event Exchange Plugin. This is a exchange at which your server app can connect and will receive internal events of RabbitMQ. You would be interested in the consumer.created and consumer.deleted events.

    When these events are received you can trigger an action (create or delete the queue you need). More information here: https://www.rabbitmq.com/event-exchange.html

    Hope this helps.


    如果您的客户允许在代理的启动/停止过程中动态绑定/取消绑定队列,则应该可以通过这种方式(例如,队列已预先设置,并且客户在启动期间将队列绑定到想要从中接收消息的交换)

    链接地址: http://www.djcxy.com/p/59244.html

    上一篇: 通过.NET客户端的持久性消息的RabbitMQ延迟

    下一篇: RabbitMQ:当没有消费者连接时丢弃消息