Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check whether a RabbitMQ message queue exists or not?

How can I check whether a message Queue already exists or not?

I have 2 different applications, one creating a queue and the other reading from that queue.

So if I run the Client which reads from the queue first, than it crashes.
So to avoid that i would like to check first whether the queue exists or not.

here is the code snippet of how I read the queue:

QueueingBasicConsumer <ConsumerName> = new QueueingBasicConsumer(<ChannelName>);  <ChannelName>.BasicConsume("<queuename>", null, <ConsumerName>);  BasicDeliverEventArgs e = (BasicDeliverEventArgs)<ConsumerName>.Queue.Dequeue(); 
like image 904
Jigar Sheth Avatar asked Aug 11 '10 10:08

Jigar Sheth


People also ask

How do I check if a queue exists?

1 Answer. Show activity on this post. Pass the flag IPC_EXCL to msgget() and if it fails and errno equals EEXIST then the queue exists.

Is RabbitMQ message in queue?

RabbitMQ is a message-queueing software also known as a message broker or queue manager. Simply said; it is software where queues are defined, to which applications connect in order to transfer a message or messages. A message can include any kind of information.


2 Answers

Don't bother checking.

queue.declare is an idempotent operation. So, if you run it once, twice, N times, the result will still be the same.

If you want to ensure that the queue exists, just declare it before using it. Make sure you declare it with the same durability, exclusivity, auto-deleted-ness every time, otherwise you'll get an exception.

If you actually do need to check if a queue exists (you shouldn't normally need to), do a passive declare of the queue. That operation succeeds if the queue exists, or fails in an error if it doesn't.

like image 165
scvalex Avatar answered Nov 07 '22 04:11

scvalex


This won't work in situations when there is someone else (other application) responsible for q declaration. And I simply could not know all the parameters of the q, just the name.

I would rather use passiveDeclare and check for the IOException that the q does not exists

like image 23
Wertikal Avatar answered Nov 07 '22 04:11

Wertikal