Search code examples
c#.netrabbitmqrabbitmq-exchange

How / When to remove queues in RabbitMQ .net Client


Working with .net rabbitmq client (https://www.rabbitmq.com/dotnet.html). Created a library that other projects can reference to subscribe/unsubscribe/publish messages on the bus.

Based on the info provided here and here, in order to have each subscriber receive all messages, each client needs to define a different queue per subscriber, bound to the exchange. The issue I'm having is how to remove queues after the "client" app is done using the queue (as this would grow exponentially and never get cleaned up).

Would it be in Dispose method? What would be the best approach?

public interface IEventBus
{
    void Publish(DomainEvent @event);

    void Subscribe<TH, T>()
        where TH : IEventHandler<T> where T : DomainEvent;

    void Unsubscribe<TH, T>()
        where TH : IEventHandler<T> where T : DomainEvent;
}

public class EventBus : IEventBus, IDisposable
{
    ....//implementation
    // dispose connection/channel etc

Then in the client project I reference my assembly and use the bus like so:

var bus = serviceProvider.GetService<IEventBus>();
bus.Subscribe<CancelEventHandler, CancelEvent>();
...
bus.Publish(....);

Solution

  • You’re looking for an Exclusive/Autodelete queue. By definition, such a queue can only be accessed by the client that created it- and when the client disconnects, the queue is instantly removed. These are useful for cases where you set up a consumer to subscribe to a topic, and you don’t actually need messages to be enqueued on the server.