使用 EJB 3.1 Singleton Bean 作为多个远程有状态会话 Bean 的客户端

发布于 2024-12-08 09:05:15 字数 485 浏览 5 评论 0原文

我对 EJB 3.1 非常陌生,正在尝试解决服务器端问题;也许有人可以提供一些指导。

我有一个状态机,代表我的应用程序中多个用户的共享状态。我正在尝试将此状态机建模为有状态会话 Bean;由于此状态机代表多个用户,因此我引入了一个 Singleton Session bean,它是 StateMachine 的实际客户端,并且所有用户最终都成为 Singleton bean 的“客户端”。当我想在应用程序的整个生命周期中使用多个状态机的生命周期时,我的问题就出现了。

我希望我的 Singleton bean(“管理器”)能够处理客户端请求并分发到适当的 StateMachine - 我将如何访问该 Stateful bean 的特定实例?为了增加进一步的复杂性,我尝试远程访问这些 StateMachine beans(如果它是本地的,我只需创建这些东西的实例作为 Manager 的成员)。

无论如何,我希望这一点是清楚的。我觉得我错过了 EJB 设计的一些基本点;如果是这样的话,你们都会告诉我。

I'm very new to EJB 3.1 and am trying to solve a server side problem; perhaps someone could offer some guidance.

I have a state machine that represents the shared state of multiple users in my application. I'm attempting to model this state machine as a Stateful Session Bean; since there are multiple users represented by this State Machine, I introduced a Singleton Session bean that is the actual Client of the StateMachine and all of the users end up being "Clients" to the Singleton bean. My problem arises when I want to lifecycle multiple StateMachines throughout the life of the Application.

I would like my Singleton bean (the "Manager") to handle client requests and distribute to the appropriate StateMachine - how would I access specific instances of that Stateful bean? To add further complexity, I'm trying to access these StateMachine beans remotely (if it were local, I'd just create instances of these things as members of the Manager).

Anyway, I hope this is clear. I feel like I'm missing some fundamental point of EJB design; y'all will tell me if that's the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-12-15 09:05:15

EJB 3.1 中引入了单例,提供了在多个实例之间共享状态的能力,如 EJB 示例中所述3.1

单身人士

EJB API 中的一个长期遗漏是能够轻松地
在企业 Bean 组件的多个实例之间共享状态
或应用程序中的多个企业 Bean 组件之间。经过
相比之下,Java EE Web 应用程序编程模型始终
通过 ServletConfig 对象提供这种类型的功能。在
EJB 3.1,这个遗漏已通过引入解决
Singleton Bean,也称为单例。

单例是一种新型会话 Bean,保证
为特定 Java Virtual 中的应用程序实例化一次
机器(JVM)*。单例是使用 @Singleton 定义的
注解,如下代码示例所示:

@Singleton 公共类 PropertiesBean {

私有属性道具;
私有 int 访问计数 = 0;

公共字符串 getProperty(字符串名称) { ... }

公共 int getAccessCount() { ... }

} 因为它只是会话 bean 的另一种风格,单例可以
将相同的本地和远程客户端视图定义为无状态和
有状态的 Bean。客户端访问单例的方式与它们相同
访问无状态和有状态 Bean,即通过 EJB
参考。例如客户端可以访问上面的PropertiesBean
单例如下:

@EJB private PropertiesBean propsBean;

...

String msg = propsBean.getProperty("hello.message");在这里,
容器确保对所有 PropertiesBean 的所有调用
同一 JVM 中的引用由同一实例提供服务
属性Bean。默认情况下,容器强制执行相同的线程
保证与其他组件类型相同。具体来说,不超过一个
允许在任何一个调用访问特定的 bean 实例
时间。对于单例来说,这意味着阻止任何并发调用。
然而,这只是默认的并发行为。有
额外的并发选项,允许更高效的并发
访问单例实例。

查看Java EE6 事件了解如何使用事件发送通知。

Singletons have been introduced in EJB 3.1 providing the ability to share state between multiple instances as described in A Sampling of EJB 3.1.

Singletons

A long-standing omission in the EJB API has been the ability to easily
share state between multiple instances of an enterprise bean component
or between multiple enterprise bean components in the application. By
contrast, the Java EE web application programming model has always
provided this type of capability through a ServletConfig object. In
EJB 3.1, this omission has been addressed with the introduction of
singleton beans, also known as singletons.

A singleton is a new kind of session bean that is guaranteed to be
instantiated once for an application in a particular Java Virtual
Machine (JVM)*. A singleton is defined using the @Singleton
annotation, as shown in the following code example:

@Singleton public class PropertiesBean {

private Properties props;
private int accessCount = 0;

public String getProperty(String name) { ... }

public int getAccessCount() { ... }

} Because it's just another flavor of session bean, a singleton can
define the same local and remote client views as stateless and
stateful beans. Clients access singletons in the same way as they
access stateless and stateful beans, that is, through an EJB
reference. For example, a client can access the above PropertiesBean
singleton as follows:

@EJB    private PropertiesBean propsBean;

...

String msg = propsBean.getProperty("hello.message"); Here, the
container ensures that all invocations to all PropertiesBean
references in the same JVM are serviced by the same instance of the
PropertiesBean. By default, the container enforces the same threading
guarantee as for other component types. Specifically, no more than one
invocation is allowed to access a particular bean instance at any one
time. For singletons, that means blocking any concurrent invocations.
However, this is just the default concurrency behavior. There are
additional concurrency options that allow more efficient concurrent
access to the singleton instance.

Have a look at Java EE6 Events about how to send notifications using events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文