【问题标题】:RabbitMQ notification on queue removalRabbitMQ 队列移除通知
【发布时间】:2019-12-09 18:04:00
【问题描述】:

我们尝试在 dotnet 中的 RabbitMQ 上删除队列时收到通知。 RabbitMQ 的“启用 rabbitmq_event_exchange”命令似乎启用了它。 https://www.rabbitmq.com/event-exchange.html

我们使用下面链接中的 (java) 示例作为 .net 实现的灵感。

https://github.com/rabbitmq/rabbitmq-event-exchange/blob/master/examples/java/QueueEvents.java

这是我们目前实现的基本功能:

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;

namespace *.QueueListener
{
    public class CustomEventingBasicConsumer : EventingBasicConsumer
    {
        public CustomEventingBasicConsumer(IModel model) : base(model)
        {
        }

        public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool 
            redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
        {
            Console.WriteLine(routingKey);
            base.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, 
                properties, body);
        }
    }
}

我们希望收到路由键的通知。正如 if (event.equals("queue.created")) 的 java 示例中所发生的那样,但是,除了我在另一边发布的消息之外,没有记录任何消息。

关于我们如何接收这些消息有什么想法吗?

(我们使用的是 RabbitMQ 3.7.8)

【问题讨论】:

  • 您是否启用了rabbitmq_event_exchange 插件?

标签: .net rabbitmq


【解决方案1】:

你连接到 amq.rabbitmq.event 交换了吗?有关队列删除的事件在此交换中发布

可以使用 ExchangeDeclarePassive 连接到此交易所。

    public static void AddChannelToEventConnection()
    {
        _eventChannel = PublisherConnection.CreateModel();
        _eventChannel.ExchangeDeclarePassive("amq.rabbitmq.event");

        _eventChannel.BasicQos(0, 1, false);
        _eventConsumer = new CustomEventingBasicConsumer(_eventChannel);
    }

之后,您想要通知的所有队列都需要绑定到交换器

_eventChannel.QueueBind("queue", "amq.rabbitmq.event", "queue", null);

最后你需要创建一个自定义事件消费者

public class CustomEventingBasicConsumer : EventingBasicConsumer
{
    public CustomEventingBasicConsumer(IModel model) : base(model)
    {
    }

    public override void HandleBasicCancelOk(string consumerTag)
    {
        base.HandleBasicCancelOk(consumerTag);
    }
    public override void HandleBasicCancel(string consumerTag)
    {
        base.HandleBasicCancel(consumerTag);
    }

    public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
    {
        base.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);
    }

    public override void HandleBasicConsumeOk(string consumerTag)
    {
        base.HandleBasicConsumeOk(consumerTag);
    }

}

如果您在 HandleBasicCancel 中放置断点,您将看到在队列删除时断点将被队列的 consumerTag 命中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2015-06-13
    • 2018-05-04
    • 2012-10-27
    • 2021-06-14
    相关资源
    最近更新 更多