'Is it possible to see the names / tags of all active consumers of a queue using the RabbitMQ .NET client?

I am creating a .NET application that will consume from several third-party queues that my application does not create or maintain, and I would like to make it so there is one consumer per queue per instance of the process. When I create a consumer, I can give it a tag for the current process ID, but I don't see any way to retrieve that tag later, unless I hold on to the consumer object.

There are a few ways I can think of in which I can track currently open channels myself, but it would be much nicer if I could check the queue to see if there is already an active consumer on a channel that has a tag with the process ID. IBasicConsumer has a ConsumerTags property which just returns the tags for that consumer, I don't see a way to access tags for other consumers.

I also see that IModel has a ConsumerCount() method, but that seems to return the total number of consumers on the queue, not just for a given channel. Even then, it is the consumer count only, and no other information about them.

IModel channel = connection.CreateModel();

// Only returns the current consumer's tags
var consumer = new AsyncEventingBasicConsumer();
string[] tags = consumer.ConsumerTags;

// Only returns count of total consumers
int count = channel.ConsumerCount("my.queue");

// This also returns the tag for the consumer used for the operation
string tag = channel.BasicConsume(consumer, "my.queue", true, "MyTag");
tag == "MyTag"; // true

// Hoping to do something like this
//var tags = channel.GetConsumers().SelectMany(c => c.ConsumerTags);

Is there a way for me to poll the queue and return the tags of any current consumers?



Solution 1:[1]

Your application can get quite a bit of information via RabbitMQ's HTTP API - https://rawcdn.githack.com/rabbitmq/rabbitmq-server/v3.9.14/deps/rabbitmq_management/priv/www/api/index.html

I'm not 100% sure if the consumer tag is exposed, however.

Otherwise, perhaps using a queue dedicated to coordinating consumers would be a good option.


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luke Bakken