MSMQ 一对多(侦听器)场景
我有这样的场景:一个客户端向 msmq 队列实例发送一条消息,并且有 3 个进程侦听该队列。我希望能够让每个实例选择不同的消息并处理它。
我知道这是队列的常见使用场景,并且我已经使用 MSMQ、.NET 和 C# 编写了工作代码。
但是我想知道 msmq 是否是我的最佳选择 - 文档明确指出 MSMQ 用于“一对一”通信,这意味着不应有多个侦听器。
这让我想知道,我正在做的解决方案是否适合我的用例?或者是相反,我是否必须为每个侦听器创建一个队列并在工作流程的前面部分分发消息?
非常感谢演示 MSMQ 在此类场景中的使用的工作示例的链接。
谢谢
I have this scenario: One client sends a message into a msmq queue instance and there are 3 processes which listen on this queue. I want to be able to let every one of those instances pick a different message and process it.
I know that is a common usage scenario for queues and i already have working code for this using MSMQ, .NET and C#.
However i am wondering if msmq is my best option here - the documentation clearly states that MSMQ is meant for "one to one" communication, meaning that there shouldnt be more than one listener.
That kind of leaves me wondering, is what i am doing the right solution for my use case? Or is it the other way round, do i have to create one queue per listener and distribute the messages in a preceeding part of the workflow?
A link to a working example demonstrating the usage of MSMQ in this type of scenario would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,您正在使用多个侦听器来执行负载平衡之类的操作。这是一个绝对有效的场景,通常用于集群环境或单个侦听器无法消耗所有传入消息的负载平衡场景。顺便提一句。群集 BizTalk 以相同的方式使用 MSMQ 消息。
一对一意味着一条消息传递给一个侦听器,但这并不意味着每个队列只能有一个侦听器。如果所有侦听器都进行相同的处理,并且不取决于哪一个侦听器将选择消息,那么它仍然是一对一的。
也可以使用一个队列将一条消息传递给多个侦听器。尽管在技术上可以通过触发器实现这种情况,但不建议使用 MSMQ。
如果您的侦听器仅侦听具有某些特殊属性的消息,确定哪个侦听器应该使用该消息(即您在队列中搜索消息),那么您绝对应该使用三个队列。
As I understand it you are using multiple listeners to do something like load balancing. This is an absolutely valid scenario and it is often used in clustered environments or in load balancing scenarios where a single listener is not able to consume all incoming messages. Btw. clustered BizTalk consumes MSMQ messages in the same way.
The one-to-one is meant as one message is passed to one listener but it doesn't mean that each queue can have only single listener. If all listeners do the same processing and it doesn't depend which one will pick the message, it is still one-to-one.
It is also possible to use one queue to deliver one message to multiple listeners. This scenario is not recommended with MSMQ even though it is technically possible with triggers.
If your listeners listen only for messages with some special properties, identifying which listener should consume the message (i.e. you search for messages in the queue), you should definitely use three queues instead.
“文档明确指出 MSMQ 用于“一对一”通信,这意味着不应有超过一个侦听器。”
你有这个的链接吗?
MSMQ 使用两种传递方法:
1-1:一个发送者,一个目标队列
1-M:一个发送方多播到多个目标队列
此外,您可以在一个队列上拥有多个侦听器。
听众的数量取决于你。
当然,多个侦听器之间会存在争用,因此如果您希望仅处理一次消息,则需要为此进行编码/配置。
"the documentation clearly states that MSMQ is meant for "one to one" communication, meaning that there shouldn't be more than one listener."
You have a link for this?
MSMQ uses two delivery methods:
1-1 : one sender, one destination queue
1-M : one sender multicasting to many destination queues
Also, you can have multiple listeners on a queue.
The number of listeners is up to you.
Of course, there will be contention between multiple listeners so if you want messages to be processed only once you need to code/configure for that.
听起来您需要一个服务总线 - 然而,它们往往有点重量级,所以它可能有点矫枉过正。通过服务总线,您可以设置发布-订阅场景,其中任意数量的侦听器都可以订阅消息。 NServiceBus 是一个使用起来比较简单的服务总线(并且它构建在 MSMQ 之上);它有一个免费版本,每秒最多发送 30 条消息。 Rhino ESB 也声称是轻量级的。
It sounds like you need a service bus - however, they tend to be somewhat heavyweight, so it might be overkill. With a service bus, you can set up publish-subscribe scenarios in which any number of listeners can subscribe to messages. NServiceBus is a service bus that is somewhat simple to use (and it is built on top of MSMQ); there is a free version of it that is capped to 30 messages per second. Rhino ESB also claims to be lightweight.