开发者

CDI Events, scope of their propagation

开发者 https://www.devze.com 2023-03-22 21:37 出处:网络
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 b

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开发者_C百科 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消