测试远程 MessageQueue 是否存在(使用 C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1010366/
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-08-06 05:28:37  来源:igfitidea点击:

Testing Whether a Remote MessageQueue Exists (using C#)

c#msmq

提问by user89166

How can I tell whether a remote message queue exists? The documentation states that the "Exists" method does not work for remote machines.

如何判断远程消息队列是否存在?文档指出“Exists”方法不适用于远程机器。

The following is not valid (I know the queue path is accurate since I am able to send messages to the queue):

以下无效(我知道队列路径是准确的,因为我能够向队列发送消息):

if (!MessageQueue.Exists(@"FormatName:Direct=TCP:192.168.2.58\Private$\MyQueue"))
  throw new InvalidOperationException("Queue does not exist");

The problem is that sending a message to a network address that does not have a listening queue behind it does not cause an exception. Having an exception thrown for an invalid queue address is critical to our application.

问题是将消息发送到后面没有侦听队列的网络地址不会导致异常。为无效的队列地址抛出异常对我们的应用程序至关重要。

采纳答案by Tetraneutron

There is an article about this:

有一篇关于这个的文章:

Frank's alternative approach is to make use of other features that MSMQ provides, such as negative acknowledgements messages with administration queues.

What should happen is that either:

  • the message will be delivered successfully to the destination queue
  • a negative acknowledgement (NACK) will be returned to the administration queue with a class of "The destination queue does not exist." (MQMSG_CLASS_NACK_BAD_DST_Q) Alternatively you could use negative source journaling and, on failure to deliver, should see the same class of NACK in the corresponding "Dead-letter messages" system queue.

In summary, don't check if the queue exists but instead handle the non-delivery of the message should it turn out that the queue doesn't exist.

Frank 的替代方法是利用 MSMQ 提供的其他功能,例如带有管理队列的否定确认消息。

应该发生的是:

  • 消息将成功传递到目标队列
  • 将向管理队列返回一个否定确认 (NACK),其类别为“目标队列不存在”。(MQMSG_CLASS_NACK_BAD_DST_Q) 或者,您可以使用负源日志记录,并且在交付失败时,应该在相应的“死信消息”系统队列中看到相同类别的 NACK。

总之,不要检查队列是否存在,而是在结果队列不存在的情况下处理未传递的消息。

http://blogs.msdn.com/johnbreakwell/archive/2008/07/31/checking-if-msmq-queues-exist-is-hard-work-so-should-you-bother.aspx

http://blogs.msdn.com/johnbreakwell/archive/2008/07/31/checking-if-msmq-queues-exist-is-hard-work-so-should-you-bother.aspx

回答by Sesh

What if the remote Q was controlled by a third party where you do not have any control?

如果遥控器 Q 由您无法控制的第三方控制怎么办?

You'd better have some kind of timeout checks instead of relying on remote info.

您最好进行某种超时检查,而不是依赖远程信息。