HdG
HdG

Reputation: 63

RabbitMQ notification on queue removal

We tried to get a notification when a queue is removed on RabbitMQ in dotnet. The "enable rabbitmq_event_exchange" command of RabbitMQ seems to enable that. https://www.rabbitmq.com/event-exchange.html

We used the (java) example in the link below as inspiration for the .net implementation.

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

This is the basic function we implemented so far:

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);
        }
    }
}

We expected to get notified by the routing key. As happens in the java example with if (event.equals("queue.created")) However, there are no messages logged, besides the one published by myself at the other side.

Any ideas on how we can receive these messages?

(We are using RabbitMQ 3.7.8)

Upvotes: 0

Views: 554

Answers (1)

Robin Timmermans
Robin Timmermans

Reputation: 26

Did u connect to the amq.rabbitmq.event exchange? events concerning queue deletes are published on this exchange

Its possible to connect to this exchange by using ExchangeDeclarePassive.

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

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

After that all queues on which you want to be notified need to be binden to the exchange

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

Finally u need to create a custom event consumer

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);
    }

}

If you put a breakpoint in HandleBasicCancel You will see that on queue deletion the breakpoint will be hit with the consumerTag of the queue

Upvotes: 1

Related Questions