CDI 事件及其传播范围

发布于 2024-11-26 08:33:32 字数 440 浏览 1 评论 0原文

这是我经常遇到的问题,我想也许 CDI 活动可以帮助我,但我不确定。

我有两个用户在网站中进行交互,我希望他们共享一个 bean 实例,以便他们都可以共享一个活动。到目前为止,我知道如何做到这一点的唯一方法是将数据推送到数据库,然后使用两个不同的 bean,每个用户一个,持续检查更改。

我的问题是,如果一个 sessionscoped bean 观察到一个事件,当我触发它时,每个用户的每个 sessionbean 都会收到通知吗?或者只有活动用户的会话 bean?因为这样我就可以使用观察者来使两个用户的对象保持同步。然而,我真的不认为这是它的工作方式,因为如果我有 1000 个会话触发一个事件,就会产生 1000 次方法调用......

我的另一个解决方案是一个巨大的应用程序作用域 bean,它保存两个活动对象用户,那么对其所做的任何更改都可以传达给用户,但是,我仍然必须扫描这个对象,我是否遗漏了一些东西?

Here's a recurrent problem that I have and I think maybe CDI events can help me but I'm not sure.

I have two users interacting in a website, I want them to share an instance of a bean so they can both share an activity. So far the only way I know how to do this is by pushing data to the database then having two different beans, one for each user, continuosly check for changes.

My question is, if a sessionscoped bean observes an event, do every sessionbean of every user get notified when i fire it? Or only the session bean for the active user?. Because then I could use observer to keep an object syncronized for both users. However I don't really think this is the way it works because if I have a thousand sessions firing an event would incur in a 1000 method calls...

My other solution would be a huge application scoped bean that holds the activity object for both users, then any change made to it can be communicated to the users, but, I still have to be scanning this object, am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忆离笙 2024-12-03 08:33:32

您不会像这样免费获得它,因为当事件被触发时,当前线程只有一个会话处于活动状态,并且[如果非静态]调用观察者方法的实际对象是从活动中获得的语境。

You won't get it for free like that, because when the event is fired one and only one session is active for the current thread, and the actual object the observer method is called on [if non-static] is obtained from the active context.

后来的我们 2024-12-03 08:33:32

您可以通过使用所有会话都可以看到的 @ApplicationScoped bean 并将其用作“缓存”来解决此问题。任何会话都可以触发事件,并且 @ApplicationScoped bean 可以 @Observe 它,并且您可以将此 bean 的引用注入到 @SessionScoped 用户 bean 中。由于 @ApplicationScoped 从技术上讲在所有登录用户 CurrentContext 中都可用,因此它可以接收来自任何会话的事件。

You could solve this by having an @ApplicationScoped bean which all sessions could see and use it as a "cache". Any session can fire an event and the @ApplicationScoped bean can @Observe it and you can inject this bean's reference into your @SessionScoped user bean. Because @ApplicationScoped is technically available in all logged in users CurrentContext it can receive events from any session.

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