【问题标题】:How / When to remove queues in RabbitMQ .net Client如何/何时在 RabbitMQ .net 客户端中删除队列
【发布时间】:2019-11-08 15:56:48
【问题描述】:

使用 .net rabbitmq 客户端 (https://www.rabbitmq.com/dotnet.html)。创建了一个库,其他项目可以引用该库以在总线上订阅/取消订阅/发布消息。

根据herehere 提供的信息,为了让每个订阅者接收所有消息,每个客户端需要为每个订阅者定义一个不同的队列,绑定到交换器。我遇到的问题是如何在“客户端”应用程序使用队列完成后删除队列(因为这会成倍增长并且永远不会被清理)。

会在 Dispose 方法中吗?最好的方法是什么?

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

然后在客户端项目中我引用我的程序集并像这样使用总线:

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

【问题讨论】:

    标签: c# .net rabbitmq rabbitmq-exchange


    【解决方案1】:

    您正在寻找独占/自动删除队列。根据定义,这样的队列只能由创建它的客户端访问 - 当客户端断开连接时,队列会立即删除。这些对于您设置消费者订阅主题的情况很有用,并且您实际上不需要在服务器上排队消息。

    【讨论】:

    • 这对我有用:channel.QueueDeclare(_queueName, true, true, true, null); - 设置自动删除,TRUE 独有
    猜你喜欢
    • 2013-11-23
    • 2022-01-20
    • 1970-01-01
    • 2011-10-08
    • 2015-05-06
    • 2014-09-14
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多