你如何从 C# 客户端获取 RabbitMQ 队列大小?

The*_*his 3 c# rabbitmq

我需要设置一个队列中可以有多少消息的上限。所以显然我需要知道队列中有多少项目。如何在不点击管理 API 或使用 QueueDeclarePassive 的情况下从 c# 客户端检查 RabbitMQ 队列中的消息数量?

The*_*his 6

下面是 IModel 对象上的消息计数函数的示例。您不需要使用 QueueDeclarePassive 或向管理插件发出休息请求。有一个函数就在它应该在的地方。

public uint GetMessageCount(string queueName)
{
    using (IConnection connection = factory.CreateConnection())
    using (IModel channel = connection.CreateModel())
    {
        return channel.MessageCount(queueName);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于文档:http : //www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.6.4/rabbitmq-dotnet-client-3.6.4-client-htmldoc/html/type-RabbitMQ.Client.IModel.html #method-M:RabbitMQ.Client.IModel.MessageCount(System.String)