使用 WCF MSMQ 的观察者模式

发布于 2024-07-08 06:56:16 字数 570 浏览 9 评论 0原文

是否可以对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め可乐爱微笑 2024-07-15 06:56:16

我不认为 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.

对风讲故事 2024-07-15 06:56:16

我认为您需要一个发布-订阅频道

发送者如何向所有感兴趣的接收者广播事件?
在发布-订阅通道上发送事件,该通道会传送特定事件的副本
事件发送给每个接收者。

发布-订阅通道的工作原理如下:它有一个输入通道
分成多个输出通道,每个通道对应一个订阅者。
当事件发布到通道中时,发布-订阅
通道将消息的副本传递到每个输出通道。
每个输出通道只有一个订阅者,仅允许订阅者
消费一条消息一次。 这样,每个订阅者只能获得
一次消息和消耗的副本将从其渠道中消失。

此模式由 MassTransitNServiceBus

I think you need a Publish-Subscribe Channel:

How can the sender broadcast an event to all interested receivers?
Send the event on a Publish-Subscribe Channel, which delivers a copy of a particular
event to each receiver.

A Publish-Subscribe Channel works like this: It has one input channel
that splits into multiple output channels, one for each subscriber.
When an event is published into the channel, the Publish-Subscribe
Channel delivers a copy of the message to each of the output channels.
Each output channel has only one subscriber, which is only allowed to
consume a message once. In this way, each subscriber only gets the
message once and consumed copies disappear from their channels.

This pattern is implemented on top of MSMQ by MassTransit and NServiceBus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文