如何检查RabbitMQ消息队列是否存在?

时间:2022-03-21 17:37:10

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.

我有2个不同的应用程序,一个创建队列,另一个从该队列读取。

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();

4 个解决方案

#1


41  

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.

queue.declare是幂等操作。所以,如果你运行一次,两次,N次,结果仍然是相同的。

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.

如果您确实需要检查是否存在队列(通常不需要),请执行队列的被动声明。如果队列存在,则该操作成功;如果队列不存在,则失败。

#2


1  

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.

当有其他人(其他应用程序)负责q声明时,这将不起作用。而我根本无法知道q的所有参数,只是名称。

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

我宁愿使用passiveDeclare并检查q不存在的IOException

#3


0  

Currently you can know that info and much more throught RabbitMQ Management HTTP API.

目前,您可以通过RabbitMQ Management HTTP API了解该信息以及更多信息。

For example, to know if one queue is up at this moment, you can invoke to GET /api/queues/vhost/name interface of the API.

例如,要知道此时是否有一个队列,您可以调用API的GET / api / queues / vhost / name接口。

#4


0  

Use QueueDeclare() to perform this as suggested. Also, what we have always done, is make the consumer of the queue be the owner of the queue, and always publish to Exchanges which are created and owned by publishers. Consumers then bind their queues to the exchanges that they wish to receive traffic from and use an appropriate route key filter for the traffic they want. In this way, publishers are muted by no consumers for non-durable queues, and consumers are free to come and go with durable or non-durable queues mapped with the appropriate route keys.

使用QueueDeclare()按照建议执行此操作。此外,我们一直在做的是使队列的使用者成为队列的所有者,并始终发布到由发布者创建和拥有的Exchange。然后,消费者将他们的队列绑定到他们希望从其接收流量的交换机,并为他们想要的流量使用适当的路由密钥过滤器。通过这种方式,发布者对非持久队列没有消费者的静音,消费者可以随意使用适当的路由键映射的持久或非持久队列。

This results in an easily administered system and allows web administration to be used to create a durable queue and bind it to an exchange, get some traffic, unbind it, and then inspect the queue contents to understand what traffic and load is coming through the exchange.

这导致了一个易于管理的系统,并允许使用Web管理来创建持久队列并将其绑定到交换机,获取一些流量,解除绑定,然后检查队列内容以了解通过交换机传输的流量和负载。

#1


41  

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.

queue.declare是幂等操作。所以,如果你运行一次,两次,N次,结果仍然是相同的。

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.

如果您确实需要检查是否存在队列(通常不需要),请执行队列的被动声明。如果队列存在,则该操作成功;如果队列不存在,则失败。

#2


1  

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.

当有其他人(其他应用程序)负责q声明时,这将不起作用。而我根本无法知道q的所有参数,只是名称。

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

我宁愿使用passiveDeclare并检查q不存在的IOException

#3


0  

Currently you can know that info and much more throught RabbitMQ Management HTTP API.

目前,您可以通过RabbitMQ Management HTTP API了解该信息以及更多信息。

For example, to know if one queue is up at this moment, you can invoke to GET /api/queues/vhost/name interface of the API.

例如,要知道此时是否有一个队列,您可以调用API的GET / api / queues / vhost / name接口。

#4


0  

Use QueueDeclare() to perform this as suggested. Also, what we have always done, is make the consumer of the queue be the owner of the queue, and always publish to Exchanges which are created and owned by publishers. Consumers then bind their queues to the exchanges that they wish to receive traffic from and use an appropriate route key filter for the traffic they want. In this way, publishers are muted by no consumers for non-durable queues, and consumers are free to come and go with durable or non-durable queues mapped with the appropriate route keys.

使用QueueDeclare()按照建议执行此操作。此外,我们一直在做的是使队列的使用者成为队列的所有者,并始终发布到由发布者创建和拥有的Exchange。然后,消费者将他们的队列绑定到他们希望从其接收流量的交换机,并为他们想要的流量使用适当的路由密钥过滤器。通过这种方式,发布者对非持久队列没有消费者的静音,消费者可以随意使用适当的路由键映射的持久或非持久队列。

This results in an easily administered system and allows web administration to be used to create a durable queue and bind it to an exchange, get some traffic, unbind it, and then inspect the queue contents to understand what traffic and load is coming through the exchange.

这导致了一个易于管理的系统,并允许使用Web管理来创建持久队列并将其绑定到交换机,获取一些流量,解除绑定,然后检查队列内容以了解通过交换机传输的流量和负载。