访问无状态内部有状态的现有实例,java ee 6 [英] access existing instance stateful inside stateless, java ee 6

查看:15
本文介绍了访问无状态内部有状态的现有实例,java ee 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问无状态 bean 中的有状态会话 bean?

Is it possible to access a stateful session bean inside a stateless bean?

我的问题是我有一个名为 User 的会话 bean,我想访问无状态 bean 中的用户信息......

My problem is that I have a session bean called User and I want to access user info inside a stateless bean...

我正在尝试这样:

@Stateless
public class OfferManagerBean implements OfferManagerLocal, OfferManager
{
    @Resource 
    private SessionContext context;
    @EJB
    private ro.project.ejb.interfaces.User user;
    public String getUsername()
    {
        user = (ro.project.ejb.interfaces.User) context.lookup("java:global/project/projectEJB/User!ro.project.ejb.interfaces.User");
        return user.getUsername();
}

客户端

 User user = (User) ctx.lookup("java:global/project/projectEJB/User!ro.project.ejb.interfaces.User");
 user.setUsername("Alex");

 OfferManager offerManager = (OfferManager) ctx.lookup("java:global/project/projectEJB/OfferManagerBean!ro.project.ejb.interfaces.OfferManager");
 assertEquals(offerManager.getUsername(), "Alex");

这个测试用例的结果是java.lang.AssertionError: expected:但是是:

它失败了.. 似乎我请求有状态 bean 的方式正在返回一个新实例...

it fails.. It seems that how I am requesting the stateful bean is returning me a new instance...

  1. 我知道为什么这不起作用.因为我的测试失败了:P.我得到了一个新实例..
  2. 我想在 EJB 中检查登录用户的某些权限,因为我不想依赖客户端,因为我可能会在那里犯错,或者我会告诉其他开发人员为我的项目制作 GUI..
  3. 我不想使用 Java EE Security,因为我不知道如何在 RCP 应用程序中进行登录
  4. 我的主要问题是:如何访问 EJB 中的会话 bean(与客户端拥有的会话 bean 相同).. 可能吗?以及如何?

我问的问题几乎和这个人问的一样:rmi ejb 调用中可重用登录会话的概念

I am asking almost the same thing this guy is asking: Concept for reusable login session in rmi ejb calls

我想这样做,但不想使用 JAAS...

I want to do that but not with JAAS...

提前致谢

推荐答案

您应该永远@Stateless<中注入 @Stateful bean (SFSB)/code> bean (SLSB).SFSB 的生命周期与其客户端的生命周期一样长(客户端是注入 SFSB 的实例,在本例中是 SLSB 本身).然而,SLSB 旨在成为无状态,并且大多数容器将它们放在一个池中.因此,每当 SLSB 在使用后返回池中时,它将在其他地方完全重用,但它拥有与第一次创建 SLSB 时相同的 SFSB 实例!这可能会导致不良结果.

You should never inject a @Stateful bean (SFSB) in a @Stateless bean (SLSB). A SFSB lives as long as its client lives (the client is the instance where the SFSB is been injected, which is in this case the SLSB itself). SLSB's are however intented to be stateless and most containers have them in a pool. So whenever the SLSB goes back to the pool after use, it will be reused entirely elsewhere, but it holds the same SFSB instance as it was when the SLSB was been created for the first time! This may lead to undesireable results.

此外,每次当您从 JNDI 获得 SFSB 时,您都会获得一个全新实例,这与 在其他地方共享的 SLSB 不同.SFSB 的客户端就是当前的客户端类实例,您从 JNDI 获取 SFSB.您应该自己保留这个实例并重用相同的实例,直到您完成对它的事务处理.其中一种方法是自己将其存储在 HTTP 会话中,或存储在您正在使用的 MVC 框架的会话范围托管 bean 中.

Also, everytime when you get the SFSB from JNDI, you will get a brand new instance which is unlike SLSBs not shared elsewhere. The SFSB's client is then the current client class instance where you've got the SFSB from JNDI. You're supposed to keep hold of this instance yourself and reuse the very same instance until you're finished with performing the transaction on it. One of the ways is storing it in the HTTP session yourself or in a session scoped managed bean of the MVC framework you're using.

功能需求对我来说并不完全清楚,因此很难给出如何解决您的特定问题的合适答案,但我的印象是您实际上需要将用户存储在HTTP 会话,不在 SFSB 中.对于会话 bean,初学者最常见的错误是它们将 EJB 上下文中的会话"错误地解释为 HTTP 会话.

The functional requirement is not entirely clear to me, so it's hard to give a suitable answer how to solve your particular problem, but I have the impression that you actually need to store the user in the HTTP session, not in a SFSB. The most common beginner's mistake as to session beans is namely that they incorrectly interpret the "session" in EJB context as being the HTTP session.

另见关于同类问题的相关答案以获得更深入的解释:JSF 请求范围的 bean 不断在每个请求上重新创建新的有状态会话 bean? 根据您的问题历史记录,您对 JSF 很熟悉,所以这个答案应该很容易理解.

See also this related answer on a question of the same kind for a more in-depth explanation: JSF request scoped bean keeps recreating new Stateful session beans on every request? According to your question history you're familiar with JSF, so this answer should be easy understandable.

这篇关于访问无状态内部有状态的现有实例,java ee 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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