Spring中如何结合观察者模式实现轮询
我正在尝试实现一个通过线程异步执行轮询的服务,然后通知观察者列表。
架构如下:多个并发 jsf 会话 bean 可以注册到单例服务,并在有新数据更新可用时由线程通知。当收到通知时,每个 jsf 会话 bean 通过访问其他会话范围的 spring bean(它们是 context.xml 中定义的服务)来执行一些操作。
我现在面临的问题是更新线程无法访问那些会话范围的 spring beans,因为它们的代理无法从更新线程解析,这是可以理解的,因为线程是在应用程序上下文之外实例化的,并且在应用程序上下文中没有可见性。可用的豆子。那么,如何在Spring中实现这样的机制呢?
我还尝试使用任务执行器 bean,希望 Spring 能够以某种方式使其成为当前上下文的一部分,但它失败了(无法解析由通知的 jsf 会话 bean 调用的会话范围的 spring bean)。
任何帮助将不胜感激。谢谢。
编辑:添加了一些有关通信流的更多信息。
每次新用户生成新会话时,都会创建相应的 jsf 会话 bean。然后,该 bean(一旦绑定到 http 会话)就会注册到 spring 服务(单例)。当至少一个 jsf 会话 bean 被注册时(因此当至少有一个观察者时),该服务将激活轮询机制。当服务发现有新的更新可用时,它将通过调用其“update()”方法来通知注册的观察者。在此方法中,每个观察者(或 jsf 会话 bean)将通过其他一些 spring 会话 bean(可用作代理)执行一些操作。这就是“通信”失败的地方,因为原始轮询线程(更新服务中的线程)不知道如何解析 spring 代理。有没有办法让轮询线程可以使用这些信息?谢谢。
I'm trying to implement a service that asynchronously performs polling via a thread and then notifies a list of observers.
The architecture is as follows: multiple concurrent jsf session beans can register to the singleton service and be notified by the thread in case of new data updates available. When notified, each jsf session bean performs some actions, by accessing to other session-scoped spring beans (which are services defined in the context.xml).
The problem I'm facing now is that the update thread cannot access those session-scoped spring beans, because their proxy cannot be resolved from the update thread, which is understandable since the thread is instantiated outside the application context and has no visibility on the available beans. But then, how can I implement such a mechanism in Spring?
I also tried using a task executor bean, hoping for Spring to make it somehow part of the current context, but it failed (the session-scoped spring beans called by the notified jsf session beans cannot be resolved).
Any help would be much appreciated. Thank you.
EDIT: Added some more information about the communication flow.
Everytime a new user generates a new session, a corresponding jsf session bean is created. This bean (once it is bound to the http session) is then registered to a spring service (a singleton). This service will activate the polling mechanism when at least one jsf session bean is registered (therefore when there's at least one observer). When the service finds out a new update is available, it will notify the registered observers, by calling their "update()" method. Inside this method each observer (or jsf session bean) will perform some actions via some other spring session beans, which are available as proxies. This is the point where "communication" fails, because the original polling thread (the one in the update service) doesn't know how to resolve the spring proxies. Is there a way to make this information available to the polling thread? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JSF 和 Spring 会话 bean 都知道的对象作为观察者怎么样?您需要在每个请求上轮询该对象。但在这种情况下,您不妨只调查原始来源。
How about using an object that is known the both the JSF and Spring session bean as the observer? You would need to poll that object on each request. But in that case you might as well just poll the original source.