消息处理程序的生命周期
消息处理程序的建议使用寿命是多少?
我当前的实现提出了一些问题,特别是在数据访问(NHibernate)方面。
我们当前的实现如下:
客户端应用程序(网络)
将消息发送到队列(内存中、msmq、azure)
工作应用程序(Windows 服务)
轮询队列,并将消息传递给注册的处理程序。
当我们初始化工作人员时,我们注册我们的处理程序。处理程序是延迟加载的(使用 Lazy
当处理程序初始化时,我们会填写它们的依赖项,并将它们保存在内存中,直到队列处理器关闭。
我们遇到的一个问题是,我们现在对队列处理器上的所有处理程序使用单个 NHibernate ISession。似乎更好的解决方案是在队列处理器的每个周期上重新创建处理程序,这意味着每个处理程序都可以拥有自己的 ISession。
推荐的方法是什么?
What is the recommended lifespan of a message handler?
My current implementation is raising a few problems, especially when it comes to data access (NHibernate).
Our current implementation is as follows:
Client Application (web)
Sends messages to a queue (in memory, msmq, azure)
Worker application (windows service)
Polls the queues, and passes the messages to the registered handlers.
When we initialize the worker, we register our handlers. The handlers are lazily loaded (using Lazy<T>
) so they are not created until the queue processor actually starts (on a different thread).
When the handlers are initialized, we fill out their dependencies and they sit in memory until the queue processor is shut down.
One issue we have is that we are now using a single NHibernate ISession for all the handlers on a queue processor. It seems that a better solution would be to recreate the handlers on each cycle of the queue processor, meaning that each handler can have it's own ISession.
What is the recommended approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于处理程序直接与处理消息的操作相关联,因此它们的生命周期应该相同。
如果您使用某种工作单元(NHibernate ISession),那么最好每个传输消息都有一个工作单元,如 xelibrion 所说。一条传输消息可以包含更多逻辑(应用程序)消息,如果这些逻辑(应用程序)消息以同一物理消息到达,则应将它们一起处理。
NServiceBus 和 NanoMessageBus 就是这样做的。
Since the handler is directly tied to the operation of handling a message their lifetime should be the same.
If you use some kind of unit of work (NHibernate ISession) it's probably best to have one per transport message as xelibrion sais. One transport message could contain more logical (application) messages which should be processed together if they arrived in the same physical message.
This is the way it's done in NServiceBus and NanoMessageBus.
我更喜欢每个传输消息的会话
I prefer session per transport message