jakarta-ee - 我是否还需要在 CDI bean 上使用 EJB @Clustered 来实现分布式应用程序

标签 jakarta-ee ejb cluster-computing cdi wildfly

我有一个在 Wildfly 9 应用程序服务器上运行的带有 EJB 3.1 和 CDI bean 的 Web 应用程序。我已经阅读了关于安装和部署 HA 独立集群环境的 Jboss 文档,并且根据 JBoss 文档,所有 EJB 都必须使用 @Cluster 注释进行注释,以便在不同的集群节点之间进行分发。我的问题是,我是否还需要像处理 EJB 一样注释我的 CDI bean?或者我只需要定义 <Distributable />我的 web.xml 上的标记和容器将把所有 CDI bean 作为分布式对象处理?

最佳答案

从 bean 的角度来看,集群是状态很重要的一个问题。想一想:如果一个 bean 不保存状态,那么跨集群复制它的目的是什么?

现在我们要讨论的问题是,您将从跨集群复制的 CDI bean 中获得什么。

  • @RequestScoped CDI bean 不需要跨集群中的节点:它是按需创建的,不会超过单个 HTTP 请求。没有太多的理由可以跨集群复制它

  • @SessionScoped CDI bean 将从复制中受益,因为它包含宝贵的 cargo : session 信息。当集群中的一个节点死亡时,应用程序将从能够将该 bean 迁移到另一个节点中受益。但是这个范围与 HTTP session 相关联,它完全由一个单独的机制复制,并且应该负责独立于 @Clusterable 注释复制 bean 中的数据。请记住,EJB,甚至 @Stateful 都没有连接到 HTTP session

  • @ViewScoped bean 也绑定(bind)到 HTTP session ,因此对 @SessionScoped bean 的好处也适用于此。

这里的总结是,无论什么机制负责在集群中复制 session ,都应该足以满足需要复制的 CDI bean。 @Clusterable 不适用于此处

编辑:the CDI spec 中进一步挖掘,它支持我的假设:您的 CDI bean 需要做的就是能够支持原始 session 激活和钝化,即可序列化,您应该没问题:

6.6.1. Passivation capable beans

A bean is called passivation capable if the container is able to temporarily transfer the state of any idle instance to secondary storage.

As defined by the EJB specification, a stateful session beans is passivation capable if:

interceptors and decorators of the bean are passivation capable, and,

the stateful session bean does not have the passivationCapable flag set to false.

As defined by the EJB specification, a stateless session bean or a singleton session bean is not passivation capable.

A managed bean is passivation capable if and only if the bean class is serializable and all interceptors and decorators of the bean are passivation capable.

A producer method is passivation capable if and only if it never returns a value which is not passivation capable at runtime.

A producer field is passivation capable if and only if it never refers to a value which is not passivation capable at runtime.

关于jakarta-ee - 我是否还需要在 CDI bean 上使用 EJB @Clustered 来实现分布式应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35114682/

相关文章:

eclipse - 无法更改tomcat url

java - war文件中的可执行文件

java - Payara 5 @JMSConnectionFactory 在使用@Stateless bean 时注入(inject)失败

java - 服务器端口 1099 和 4447 有什么区别?

运行 MATLAB 的 Bash 脚本错误

hadoop - Apache Hadoop - 节点机器差异?

java - 当我们转发请求时,web.xml 中的过滤器是否被执行?

jsf - 为什么 mojarra 2.1 在每个模块启动时扫描所有其他 war ?

glassfish - EJB3 Glassfish JNDI 查找

java - 集群环境下后台作业的输出