简体   繁体   English

从无状态Bean返回有状态Java Bean?

[英]Returning a Stateful Java Bean from a Stateless Bean?

I want to handle a login scenario as follows: 我想按如下方式处理登录方案:

  1. Client connects to a Stateless Java Bean (SLJB) and tries to login; 客户端连接到无状态Java Bean(SLJB)并尝试登录;
  2. If login succeeds, the SLJB returns to the user a Stateful Java Bean (SFJB), so that the client can continue using the application. 如果登录成功,SLJB会向用户返回有状态Java Bean(SFJB),以便客户端可以继续使用该应用程序。

I am currently doing the second step as: 我目前正在做第二步:

return new StatefulBean(some params);

Is this the right way to do it? 这是正确的方法吗? It does not seem to me as I get the exception: 在我看来,因为我得到了例外:

Class org.eclipse.persistence.internal.jpa.EntityManagerImpl is not Serializable

when running my application, and I think it is related to the described method. 在运行我的应用程序时,我认为它与所描述的方法有关。

What would be the correct way to return a reference to the SFJB from the SLJB to the client? 将对SFJB的引用从SLJB返回给客户端的正确方法是什么?

First of all, this is completely wrong: 首先,这是完全错误的:

new StatefulBean(some params)

EJB container is responsible for creating and destroying instances of beans, you should never create them manually. EJB容器负责创建和销毁bean实例,您永远不应手动创建它们。

In your scenario I would reverse the flow: the client connects to the stateful bean which might stateless session bean as a helper. 在您的场景中,我将反转流程:客户端连接到有状态bean,它可能是无状态会话bean作为帮助程序。 No need to pass beans around, client always uses the same bean. 无需传递bean,客户端总是使用相同的bean。

As Tomasz mentions, you probably need to rethink your flow. 正如Tomasz所提到的,你可能需要重新思考你的流程。

That said, you can get a hold of a new stateful instance by doing a JNDI lookup, using the portable JNDI name that us assigned to each bean at startup. 也就是说,您可以使用我们在启动时分配给每个bean的可移植JNDI名称,通过执行JNDI查找来获取新的有状态实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM