使用 WCF MSMQ 的观察者模式
是否可以对 MSMQ 携带的消息有多个侦听器?
WCF 似乎用服务来构建一切,使通信成为点对点的事情。 我想使用消息队列来缓冲另一个在数据库中记录日志的进程的传入流量。
可以有任意数量的其他进程对监视传入流量感兴趣,这肯定需要观察者模式,但我不知道如何表达观察者模式,至少不通过 WCF 使用 MSMQ。
有人可以就此给我建议吗?
关于我为什么关心的一些背景,这也可能有助于说明问题:我有一个 Windows 服务,它接受来自现场小黑匣子的连接请求。 它设置套接字,黑匣子发送描述事件的消息,并用 GPS 时间和空间位置进行注释。
我解析来自套接字流的消息数据包,并通过 MSMQ 将它们发送到另一个进程,该进程会过滤重复数据并将数据包写入数据库表中。
任何数量的后处理都将受益于响应传入流量的增量计算,并且用户工具也响应传入流量执行用户通知。
因此,我真的希望有一个进程发送消息,而多个进程接收消息。 其中一个接收器负责解析数据包内容并将消息转录到数据库中; 这显然是最终从队列中删除消息的责任的候选者,但存在如何确保这是处理消息的最后一个处理程序的问题。
Is it possible to have multiple listeners to messages carried by MSMQ?
WCF appears to frame everything in terms of services, making communication a point-to-point affair. I want to use a message queue to buffer incoming traffic for another process that records the logs in a database.
There can be be any number of other processes interested in monitoring incoming traffic, and this positively begs for Observer pattern, but I can't see how to express Observer pattern, at least not using MSMQ via WCF.
Can anyone advise me on this?
Some background on why I care, which may also serve to illustrate the problem: I have a Windows service that accepts connection requests from little black boxes in the field. It sets up sockets and the black boxes send messages describing events annotated with GPS locations in time and space.
I parse message packets from the socket stream and send them via MSMQ to another process that filters duplicates and writes the packets into a database table.
There is any amount of post-processing that would benefit from incremental computation in response to incoming traffic, and user tools perform user notification also in response to incoming traffic.
So, I'd really like to have one process sending the messages, and several receiving them. One of these receivers is responsible for parsing the packet contents and transcribing the message into a database; this is an obvious candidate for the responsibility of finally removing the message from the queue, but there is the question of how to ensure this is the last handler to process the message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为 MSMQ 本身的设计适合处理这种情况。 您只能对消息执行两件事 - Peek() 或 Get()。 AFAIK,没有一个好的方法来跟踪消息是否已被所有处理程序处理。
可能有效的方法是让转录消息的进程成为观察者,并在转录消息并写入数据库之前将消息发布给所有感兴趣的观察者(可能使用 .NET 事件架构)。 这将保证所有感兴趣的观察者都看到该消息,并且该消息会被适当地写入数据库。
I don't believe MSMQ is appropriately designed to handle this situation by itself. There are only 2 things you can do to a message - Peek() or Get(). AFAIK, there isn't a good way to track whether a message had been processed by all handlers.
What might work is to have your process that transcribes the message be the Observee, and publish the message (perhaps using the .NET Event architecture) to all interested Observers before transcribing the message and writing to the database. This would provide a guarantee that all interested Observers saw the message, and the message gets written to the DB appropriately.
我认为您需要一个发布-订阅频道:
此模式由 MassTransit 和 NServiceBus。
I think you need a Publish-Subscribe Channel:
This pattern is implemented on top of MSMQ by MassTransit and NServiceBus.