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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3457305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:36:08  来源:igfitidea点击:

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

.netqueuerabbitmqmessage-queuenot-exists

提问by Jigar Sheth

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

回答by scvalex

Don't bother checking.

不要费心检查。

queue.declareis 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.

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

回答by Wertikal

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

回答by Dani

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/nameinterface of the API.

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

回答by Grwww

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() 按照建议执行此操作。此外,我们一直在做的是让队列的消费者成为队列的所有者,并始终发布到由发布者创建和拥有的交易所。消费者然后将他们的队列绑定到他们希望从中接收流量的交换机,并为他们想要的流量使用适当的路由密钥过滤器。通过这种方式,对于非持久队列,发布者不会被任何消费者静音,并且消费者可以自由地使用映射到适当路由键的持久或非持久队列来来去去。

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 管理来创建持久队列并将其绑定到交换,获取一些流量,解除绑定,然后检查队列内容以了解通过交换的流量和负载.

回答by developer_.net

Put below code inside try catch section. If queue or exchange doesn't exist then it will throw error. if exists it will not do anything.

将下面的代码放在 try catch 部分中。如果队列或交换不存在,那么它会抛出错误。如果存在,它不会做任何事情。

  var channel = connection.CreateModel();


  channel.ExchangeDeclarePassive(sExchangeName);

  QueueDeclareOk ok = channel.QueueDeclarePassive(sQueueName);

   if (ok.MessageCount > 0)
    {
      // Bind the queue to the exchange

     channel.QueueBind(sQueueName, sExchangeName, string.Empty);
    }

回答by lambodar

There is a meta api in spring-amqp(java implementation)

spring-amqp(java实现)中有一个meta api

@Autowired
public RabbitAdmin rabbitAdmin;

//###############get you queue details##############
Properties properties = rabbitAdmin.getQueueProperties(queueName);

//do your custom logic
if( properties == null)
{
    createQueue(queueName);
}