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

How can I check whether a RabbitMQ message queue exists or not?(如何检查 RabbitMQ 消息队列是否存在?)
本文介绍了如何检查 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 消息队列是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)