CDI Events, scope of their propagation

橙三吉。 提交于 2019-12-10 15:37:41

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6835516/cdi-events-scope-of-their-propagation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!