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.
"The correct instance" is a slightly misleading wording.
What happens is this:
- The
SessionService
is invoked (probably triggered by a web request). - If it fires its
LoggedInEvent
, all registered observers are called in a synchronous way (meaning thatSessionService
will not terminate before all observers terminate). - 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