CDI事件,其传播范围 [英] CDI Events, scope of their propagation

查看:65
本文介绍了CDI事件,其传播范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我经常遇到的问题,我认为CDI事件也许可以帮到我,但我不确定。

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

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

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.

我的问题是,如果一个sessionscoped bean观察到一个事件,我触发它时是否会通知每个用户的每个sessionbean?还是仅活动用户的会话bean?因为这样我可以使用观察器使两个用户的对象保持同步。但是我真的不认为这是它的工作方式,因为如果我进行一千次会话触发一个事件将导致1000次方法调用...

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...

我的另一个解决方案将是一个巨大的应用程序范围的bean,该bean拥有两个用户的活动对象,然后对它进行的任何更改都可以传达给用户,但是,我仍然必须扫描该对象,我丢失了什么吗?

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?

推荐答案

您不会像那样免费获得它,因为当事件被触发时,当前线程只有一个会话处于活动状态,并且从活动上下文中获取观察者方法的实际对象(如果是非静态的)。

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.

这篇关于CDI事件,其传播范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆