@Observes in sessionscoped bean?

一笑奈何 提交于 2019-12-06 12:04:10

问题


is following scenario possible?

"SessionService" which is a stateless EJB fires an event "LoggedInEvent". A SessionScoped (Weld) bean "SessionBean" having a non static method observing the LoggedInEvent is called and initializes some things for that specific user.

Is the correct instance of "SessionBean" called? Are all instances called? I can not find anything in the documentation.


回答1:


"The correct instance" is a slightly misleading wording.

What happens is this:

  1. The SessionService is invoked (probably triggered by a web request).
  2. If it fires its LoggedInEvent, all registered observers are called in a synchronous way (meaning that SessionService will not terminate before all observers terminate).
  3. Each bean containing a (non-static) observer method will be instantiated (provided that the bean scope is active - which it usually is in a standard web environment(*)). This will be the case for your SessionBean. If - and only if - SessionBean has already been instantiated in your active session (there's certainly only one session active regarding the web request), then this instance will of course be used.

More details in the spec.

So, to answer you question:

Yes, the correct instance will be called. Why? Because it's the responsibility of the container to make sure that only one SessionBean-instance is associated with the active session scope.

(*): If an invocation is for example triggered by a remote ejb call, you can neither assume an active session nor an active conversation...



来源:https://stackoverflow.com/questions/6524170/observes-in-sessionscoped-bean

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