如何检查 RabbitMQ 消息队列是否存在? [英] How can I check whether a RabbitMQ message queue exists or not?

查看:436
本文介绍了如何检查 RabbitMQ 消息队列是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

不用检查了.

queue.declare 是幂等操作.所以,如果你运行一次,两次,N次,结果还是一样的.

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.

这篇关于如何检查 RabbitMQ 消息队列是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆