可以将同一个有状态会话bean实例注入到其他多个会话bean中吗? [英] Possible to inject same stateful session bean instance into multiple other session beans?
问题描述
是否可以使容器将同一个有状态会话Bean实例注入到其他多个有状态会话Bean中?
给出以下类:
@Stateful
公共类StatefulTwoBean实现StatefulTwo {
@EJB
private StatefulOne statefulOne;
}
@Stateful
公共类StatefulThreeBean实现StatefulThree {
@EJB
private StatefulOne statefulOne;
}
在上面的示例中, StatefulTwoBean 和 StatefulThreeBean 分别注入自己的 StatefulOneBean 实例。
是否可以使容器注入 StatefulOneBean 的相同实例到 StatefulTwoBean 和 StatefulThreeBean 中?
因此,您的解决方案是自己实现差异化。如何实现这一目标。我并不是说这是最漂亮的解决方案,但它确实有效。 -我们通过将Facade(EJB本身)(我称其为Facade,尽管它并没有完全覆盖Facade模式)来实现它,并使用以下代码:
公共对象调用(对象bean,
字符串methodName,
Object [] args,
Class [] parameterTypes,
UUID sessionId)抛出Throwable {
//找到会话
SessionContext sessionContext = SessionRegistry.getSession(sessionId);
//将其设置为当前
SessionRegistry.setLocalSession(sessionContext);
.....
}
重要参数是 sessionId
-这是客户端和服务器都知道的,并标识它们之间的当前连接。
在客户端上我们使用了动态代理来调用此外观。因此调用看起来像这样:
getBean(MyConcreteEJB.class).someMethod()
,一个getBean方法创建了代理,因此调用者没有
SessionRegistry
有
私有静态ThreadLocal< SessionContext> localSessionContext = new
ThreadLocal< SessionContext>();
而 SessionContext
只是一个Map set(键,值)
和 get(键)
所以现在,您可以使用 SessionContext
来代替使用 @Stateful
bean存储状态。
Is it possible to make the container inject the same stateful session bean instance into multiple other stateful session beans?
Given the following classes:
@Stateful
public class StatefulTwoBean implements StatefulTwo {
@EJB
private StatefulOne statefulOne;
}
@Stateful
public class StatefulThreeBean implements StatefulThree {
@EJB
private StatefulOne statefulOne;
}
In the above example, StatefulTwoBean and StatefulThreeBean each get injected their own instance of StatefulOneBean.
Is it possible to make the container inject the same instance of StatefulOneBean into both StatefulTwoBean and StatefulThreeBean?
The problem is this - Stateful beans' isntances are allocated by differentiating the clients that call them. Glassfish (and perhaps others) don't propagate this difference on injected beans. The EJB specification, as far as I remember, isn't clear about this.
So your solution is to implement the differentiation yourself. How to achieve this. I'm not pretending this is the most beautiful solution, but it worked. - we did it by putting a Facade (an EJB itself) (I'm calling it a facade, although it does not entirely cover the facade pattern) in front of all our EJBs, with the following code:
public Object call(Object bean,
String methodName,
Object[] args,
Class[] parameterTypes,
UUID sessionId) throws Throwable {
//find the session
SessionContext sessionContext = SessionRegistry.getSession(sessionId);
//set it as current
SessionRegistry.setLocalSession(sessionContext);
.....
}
The important parameter is sessionId
- this is something both the client and the server know about, and identifies the current seesion between them.
On the client we used a dynamic proxy to call this facade. So the calls look like this:
getBean(MyConcreteEJB.class).someMethod()
, an the getBean method created the proxy, so that callers didn't have to know about the facade bean.
The SessionRegistry
had
private static ThreadLocal<SessionContext> localSessionContext = new
ThreadLocal<SessionContext>();
And the SessionContext
was simply a Map providing set(key, value)
and get(key)
So now, instead of using @Stateful
beans to store your state, you could use the SessionContext
.
这篇关于可以将同一个有状态会话bean实例注入到其他多个会话bean中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!