Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get RabbitMQ queue size from c# client?

Tags:

c#

rabbitmq

I need to set an upper limit on how many messages can be in a queue. So obviously I need to know how many items are in a queue. How do you check the number of messages in a RabbitMQ queue from the c# client without hitting the management API or using QueueDeclarePassive?

like image 718
BillHaggerty Avatar asked Mar 09 '23 09:03

BillHaggerty


1 Answers

Below is an example of the message count function on the IModel object. You do not need to use QueueDeclarePassive or make rest request to the management plugin. There is a function right there where it should be.

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

For documentation: https://rabbitmq.github.io/rabbitmq-dotnet-client/api/RabbitMQ.Client.IModel.html#RabbitMQ_Client_IModel_MessageCount_System_String_

like image 86
BillHaggerty Avatar answered Mar 16 '23 20:03

BillHaggerty