6
votes

I'd like a way to iterate through existing queues on a rabbit server's virtual host, and output the number of messages in the queues, without hardcoding the queue names into my C# code.

Here's an example of getting the number of messages in a queue by hardcoding the queue value, using the RabbitMQ .NET Client:

using System;
using RabbitMQ.Client;

namespace RabbitMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory()
            {
                HostName = "<HostName>",
                UserName = "<UserName>",
                Password = "<Password>",
                VirtualHost = "<VirtualHost>",
                Port = 5672
            };

            var queueNameMessageCount = 0;

            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                queueNameMessageCount = channel.MessageCount("<QueueName>");
            }
        }
    }
}

Is there a way I can get a collection of queues / queue names that are on a given virtual host, using the RabbitMQ .NET Client?

Related: is there a way for be to get a collection of virtual hosts/virtual host names on a server, using the RabbitMQ .NET Client?

1
While I haven't found a way to do it through the .NET client, I have found what I was looking for by looking at the rest api directly: cdn.rawgit.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_9/… - BobbyA
Without knowing what you want this list for, needed the queue names is unlikely to be a good practice, at least not for the common messaging patterns. - istepaniuk

1 Answers

1
votes

No, not really. At least not with the AMQP client, since the protocol does not provide a way to get such listing.

You can, however, use the RabbitMQ management plugin. It provides a REST API where you can gather this information.