NServiceBus 新手 - 如何强制通用主机加载第一个订阅者,然后处理消息?
我正在测试 NServiceBus 并完成了以下场景:
1) 从发布者向主机发布消息,而主机尚未运行
2)启动主机
并希望该主机会将该消息发送给订阅者,但看起来它试图在加载订阅者之前处理消息,所以在处理它时自然还没有订阅者,所以我丢失了我的消息(为了清楚起见,删除了命名空间):
2012-02-16 13:21:09,634 1 信息 NServiceBus.Unicast.Transport .Msmq.MsmqTranspo rt [(null)] <(null)>; - 您正在运行该软件的社区版 仅支持一个线程。
2012-02-16 1321:10,918 [Worker.5] 信息 NServiceBus.Unicast.UnicastBus [(null)] <(空)> - 从发送者 Generator@USER-PC 收到消息 ItemCommand,ID 为 b591c15e-ee05-4d28-99da-1bac1a6a5ecb\2991
2012-02-16 13:21:11,021 [Worker.5] INFO NServiceBus.Unicast.UnicastBus [(null)] <(空)> - 订阅 WinFormsReceiver@USER-PC 消息类型 ItemEvent
2012-02-16 13:21:11,026 [Worker.5] 信息 NServiceBus.Unicast.UnicastBus [(null)] <(空)> - 订阅 Receiver@USER-PC 消息类型 ItemEvent
通用主机是使用建模工具生成的,因此默认配置。
更新:添加模型以更好地了解正在发生的情况,还重新表述了下面的问题
主机类型是 NServiceBusHost
我认为问题听起来应该是这样的:我可以在没有订阅者的情况下在主机中注册订阅者吗由他们自己注册或至少在开始处理消息之前检查是否所有订阅者都已订阅?以下是场景及其默认工作原理:
1) 宿主死亡
2) 接收器死亡
3) 生成器生成消息
4) 主机恢复,队列中有消息
5) 主机处理消息
6) 接收者恢复,它订阅了主机,但它的队列是空的,因为当主机有接收者的消息时,在第5步中主机不知道接收者。
因为现在,在 Host 恢复并准备好处理消息后,它不知道会有接收者,所以当它在步骤 5 中处理消息时,它不会将其发送给默认配置的接收者。
I'm testing NServiceBus and I've done following scenario:
1) published message to host from publisher, while host was not running yet
2) started host
and was hoping, that host will send that message to subscriber, but looks like it tried to handle message before loading subscribers, so naturally in time of handling it there was no subscribers yet, so I've lost my message (removed namespaces for clarity):
2012-02-16 13:21:09,634 1 INFO NServiceBus.Unicast.Transport.Msmq.MsmqTranspo
rt [(null)] <(null)> - You are running a community edition of the software which
only supports one thread.2012-02-16 1321:10,918 [Worker.5] INFO NServiceBus.Unicast.UnicastBus [(null)]
<(null)> - Received message ItemCommand, with ID b591c15e-ee05-4d28-99da-1bac1a6a5ecb\2991 from sender Generator@USER-PC2012-02-16 13:21:11,021 [Worker.5] INFO NServiceBus.Unicast.UnicastBus [(null)]
<(null)> - Subscribing WinFormsReceiver@USER-PC to message type ItemEvent2012-02-16 13:21:11,026 [Worker.5] INFO NServiceBus.Unicast.UnicastBus [(null)]
<(null)> - Subscribing Receiver@USER-PC to message type ItemEvent
Generic host is generated by using modeling tools, so default configuration.
Update: added model to understand better what is going on, also rephrased question below
Host type is NServiceBusHost
I think question should sound like this: can I register subscribers in host, not having subscribers to register by them selves or at least to check, if all subscribers are subscribed before starting to handle message? Here is scenario and how it works by default:
1) Host dies
2) Receiver dies
3) Generator generates message
4) Host is recovered, it has message in its queue
5) Host handles message
6) Receiver is recovered, it subscribes to host, but it's queue is empty, as host didn't knew about receiver on step 5 when it had message for receiver.
Because right now, after Host is recovered and it is ready to handle message, it does not know, that there will be receiver, so when it handles message in step 5, it won't send it to receiver in default configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,NServiceBus 可以完全按照您的描述进行操作。您可能会遇到问题,因为您没有在足够强大的配置文件中运行以进行测试。默认情况下,示例 NSB 项目使用内存订阅运行。这意味着如果正在发布的主机(在您的情况下为“主机”)关闭,所有订阅都会丢失。您想要查看其他配置文件,例如集成和生产配置文件。集成将订阅者保存在队列中,生产将订阅者保存在数据库中。然后 NSB 了解是否存在未运行的预期订阅(因为订阅信息已保留)。
此外,我建议如下:
1) 生成器向主机执行
Bus.Send()
2)Host处理消息然后发布
3) 接收方订阅主机
该主机必须配置为发布方。
希望这有帮助。
Yes, NServiceBus can do exactly what you have described. You may be encountering a problem because you are not running in a profile that is robust enough for your test. By default, the example NSB projects run with in-memory subscription. This means that if the host that is publishing ('Host' in your case) is shut down, all the subscriptions are lost. You want to check out other profiles like the Integration and Production profiles. Integration saves subscribers in a queue and Production saves subscribers in a database. Then NSB understands if there is an expected subscription to fulfil that is not running (because the subscription information is persisted).
Further, I would suggest the following:
1) Generator does a
Bus.Send()
to Host2) Host handles message and then does a publish
3) Receiver subscribes to Host
This that Host must be configured as a Publisher.
Hope this helps.
只需在尝试处理任何消息之前启动所有内容,即可向发布者预先注册您的订阅者。届时订阅肯定会得到评估。
Just pre-register your subscribers with your publisher by starting everything up before you try to handle any messages. The subscriptions will definitely get evaluated then.