@在sessionscoped bean中观察? [英] @Observes in sessionscoped bean?

查看:93
本文介绍了@在sessionscoped bean中观察?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能出现以下情况?

作为无状态EJB的"SessionService"触发事件"LoggedInEvent".具有观察LoggedInEvent的非静态方法的SessionScoped(焊接)Bean"SessionBean"被调用,并为该特定用户初始化一些操作.

是否调用了"SessionBean"的正确实例?是否都调用了所有实例?我在文档中找不到任何内容.

解决方案

正确的实例"的措词有点误导.

这是怎么回事:

  1. SessionService被调用(可能是由Web请求触发的).
  2. 如果它触发其LoggedInEvent,则会以同步方式调用所有已注册的观察者(这意味着SessionService不会在所有观察者终止之前终止).
  3. 每个包含(非静态)观察者方法的bean都将被实例化(前提是bean范围是活动的-通常在标准的Web环境(*)中).您的SessionBean就是这种情况.如果-并且仅当-SessionBean已在您的活动会话中被实例化 (对于Web请求,肯定只有一个活动会话),那么当然将使用该实例.

规范中的更多详细信息.. >

所以,要回答你的问题:

是的,将调用正确的实例.为什么?因为容器的责任是确保只有一个SessionBean实例与活动会话范围相关联.

(*):如果调用例如是由远程ejb调用触发的,则您既不能假设活动会话也不可以活动会话...

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:

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

这篇关于@在sessionscoped bean中观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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