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

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

问题描述

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



我的问题是我有一个名为User的会话bean,我想访问用户信息在无国籍豆... ...



我这样做:



Ejb Side:



  @Stateless 
公共类OfferManagerBean实现OfferManagerLocal,OfferManager
{
@Resource
private SessionContext上下文;
@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);
返回user.getUsername();
}



客户端



 用户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: <空>但是:< Alex>



它失败了..似乎我如何请求有状态bean给我一个新实例。 ..


  1. 我知道为什么这不起作用。因为我的考试失败了:P。我得到一个新的实例..

  2. 我想检查EJB中登录用户的某些权限,因为我不想指望客户端因为我可能在那里犯了错误或者我会告诉其他开发人员为我的项目制作一个GUI ..

  3. 我不想使用Java EE安全因为我不知道如何在RCP中登录应用程序

  4. 我的主要问题是:如何在EJB中访问会话bean(客户端拥有的会话bean)..是否可能?怎么样?

我问这个家伙几乎要问的是: rmi ejb调用中可重用登录会话的概念



我想这样做而不是JAAS ...



提前谢谢

解决方案

你应该从不 @Stateless <中注入 @Stateful bean(SFSB) / code> bean(SLSB)。只要客户端存在,SFSB就会存在(客户端是注入SFSB的实例,在本例中是SLSB本身)。然而,SLSB的目的是无状态,并且大多数容器都将它们放在池中。因此,每当SLSB在使用后返回池中时,它将在其他地方重新使用,但它拥有与第一次创建SLSB时相同的SFSB实例!这可能会导致不希望的结果。



此外,每当您从JNDI获得SFSB时,您将获得一个全新的实例,这与SLSB 在别处共享。然后,SFSB的客户端是您从JNDI获得SFSB的当前客户端类实例。您应该自己保留此实例,并重复使用相同的实例,直到您完成对其执行事务为止。其中一种方法是将它自己存储在HTTP会话中,或者存储在您正在使用的MVC框架的会话作用域托管bean中。



功能要求并不完全清楚对我而言,很难给出一个合适的答案如何解决您的特定问题,但我认为您实际上需要将用户存储在HTTP会话中,而不是存储在SFSB中。最常见的初学者对会话bean的错误就是他们错误地将EJB上下文中的会话解释为HTTP会话。



另请参阅此相关答案同样的问题需要更深入的解释: JSF请求作用域bean继续在每个请求上重新创建新的有状态会话bean吗?根据你的问题历史,你对JSF很熟悉,所以这个答案应该很容易理解。 / p>

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

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

I am trying like this:

Ejb Side:

@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();
}

Client side

 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");

The result of this test case is java.lang.AssertionError: expected:<null> but was:<Alex>

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

  1. I know why this is not working. Because my test fails :P. I get a new instance..
  2. I want to check certain permissions of the logged in user in EJB because I don't want to count on the client side because I might do a mistake there or I will tell other developers to make a GUI for my project..
  3. I don't want to use Java EE Security beucause I don't know how to make the login in a RCP Application
  4. My main question is: How do I access a session bean (the same one the client own) inside an EJB.. is it possible? And how?

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

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

Thank you in advance

解决方案

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.

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.

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.

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天全站免登陆