请求无状态会话 Bean 的作用域上下文
EJB3 会话 bean 是否有请求范围的上下文?我的环境是Java-EE-5。
当调用 SessionFacadeBean#myBusinessMethod() 时,此示例
@Remote(SessionFacade.class) @Stateless
public class SessionFacadeBean implements SessionFacade {
@EJB
private Other bean;
public void myBusinessMethod() {
// TODO: get or create *myRequestScope*
*myRequestScope*.put("demo", Integer.valueOf( 1 ));
bean.otherBusinessMethod();
sysout(*myRequestScope*.get("demo"));
}
}
@Local(Other.class) @Stateless
public class OtherBean implements Other {
public void otherBusinessMethod() {
// TODO: get or create *myRequestScope*
*myRequestScope*.put("demo", Integer.valueOf( 2 ));
}
}
应始终打印输出“2” - 无论并行调用如何。
我没有机会使用 CDI。而且,它还应该独立于事务传播工作(因此 JCA 也不是一个选项)。
Is there a request-scoped context for EJB3 session-beans? My environment is Java-EE-5.
This example
@Remote(SessionFacade.class) @Stateless
public class SessionFacadeBean implements SessionFacade {
@EJB
private Other bean;
public void myBusinessMethod() {
// TODO: get or create *myRequestScope*
*myRequestScope*.put("demo", Integer.valueOf( 1 ));
bean.otherBusinessMethod();
sysout(*myRequestScope*.get("demo"));
}
}
@Local(Other.class) @Stateless
public class OtherBean implements Other {
public void otherBusinessMethod() {
// TODO: get or create *myRequestScope*
*myRequestScope*.put("demo", Integer.valueOf( 2 ));
}
}
should always printout "2" when invoking SessionFacadeBean#myBusinessMethod() - irrespective of parallel invocations.
I do not have the luxury of using CDI. And, it should also work independently of transaction propagation (so JCA is also not an option).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无状态 EJB,顾名思义,不存储状态,因此不存在请求范围的概念。有一个会话范围仅限于当前运行时会话上下文,您也无法在其中存储状态,因此排除了在 bean 或容器内存储状态的任何选项。
您可能会通过使用
ThreadLocal
变量,但顾名思义,其作用域为当前执行线程。根据您发布的代码,这似乎就是您想要的。这种方法的问题在于,
Thread
对象就不会被销毁;而是会被销毁。它们被返回到容器的线程池。因此,如果您在不同的执行上下文中读取ThreadLocal值,您将找到使用同一线程的先前执行上下文的值。换句话说,确保应用程序在读取值之前始终将值放入 ThreadLocal 对象中。Stateless EJBs, are their name suggests do not store state, so there is no concept of request-scope. There is a session scope that is limited to the current runtime session context, where you cannot store state as well, so that rules out any option of storing state within the bean or within the container.
You might find some luck by using
ThreadLocal
variables, but this as the name suggests, is scoped to the current thread of execution. Going by your posted code, this appears to be what you would want. The problem with this approach is that,Thread
objects are simply not destroyed once the EJB method has completed execution; they are returned to the container's thread pool. Therefore, if you read the ThreadLocal value in a different context of execution, you will find the value of the previous execution context that used the same thread. In other words, ensure that your application always puts values in the ThreadLocal object before reading them.无状态会话 bean 是否有请求范围的上下文?
简短的答案是否定的。
详细的答案是:您需要一些上下文来在业务方法的调用之间共享数据。这可能是一个设计问题。 Requestscope是Web层的一个概念。
在 Web 层中,请求、页面、会话和应用程序范围是作为 Hashmap 实现的。因此,您可以传递对 Hashmap 的引用作为上下文来共享所有数据。
另一种方法可能是使用单例(需要在节点之间共享,例如使用 ehcache)。另
迁移到 EJB 3.1 并使用 @Singleton
考虑使用有状态 Bean 并将您的请求范围放入 Bean 会话范围中,该范围在您离开会话后可以删除请求范围。
Is there a request-scoped context for stateless session-beans?
Short answer is No.
The long answer is: You need some context to share data between invocations of your business methods. This could be a design issue. Requestscope is a concept of web-tier.
In the Web-tier the request,page,session and application scope is implemented as a Hashmap. So you could pass a reference to a Hashmap as context to share all data.
Another approach could be to use a singleton (which needs to be shared between nodes e.g. using ehcache).
Migrate to EJB 3.1 and use @Singleton
Consider to use stateful Beans and put your request-scope into the beans session scope which could be removed after you leave the request scope.