infinispan - EAP 7:EJB 状态不在集群模式下的有状态会话 bean 之间复制
问题描述
我运行了两个 EAP 7.0 实例,都带有standalone-full-ha.xml
一开始是:
/opt/jboss-eap-7.0/bin/standalone.sh -Djboss.node.name=node1 -Djboss.server.base.dir=/opt/node1 -Djboss.messaging.cluster.password=message -c standalone-full-ha.xml
另一个有:
/opt/jboss-eap-7.0/bin/standalone.sh -Djboss.node.name=node2 -Djboss.server.base.dir=/opt/node2 -Djboss.messaging.cluster.password=message -c standalone-full-ha.xml -Djboss.socket.binding.port-offset=4
两者都以成功开始,我可以看到他们加入了集群频道:
[org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-6) ISPN000078: Starting JGroups channel ejb
...
[org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 72) WFLYCLINF0002: Started eap.war cache from ejb container
我有一个有状态的会话 bean:
import javax.ejb.Stateful;
@Stateful
public class Counter {
int counter;
public int getCounter() {
++counter;
return counter;
}
}
一个 JSF 应用程序范围的 bean:
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
@ApplicationScoped
@Named
public class IndexBean implements Serializable {
@Inject
transient Counter counter;
public int getCounter() {
return counter.getCounter();
}
}
一个 JSF 会话范围的 bean:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@SessionScoped
@Named
public class SessionBean implements Serializable {
int counter;
public int getCounter() {
++counter;
return counter;
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="false" version="3.1">
<distributable/>
</web-app>
和 index.xhtml:
<html
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>EAP 7</title>
</h:head>
<h:body>
<h:outputText value="#{indexBean.counter}"></h:outputText>
<br />
<br />
<h:outputText value="#{sessionBean.counter}"></h:outputText>
</h:body>
</html>
现在,当我导航到 localhost:8080/index.xhtml 上的 node1 时,我有一个网页,其中有两个从 1 开始的计数器。每次刷新页面时,它都会计数。
当我在 localhost:8084/index.xhtml 上导航到 node2 时,我希望看到来自 node1 的两个最后递增的值,但是来自 @Stateful bean 的计数器不会在 node1 的值上递增。
示例:导航到 node1:
1
1
-> 刷新节点1
2
2
-> 刷新节点1
3
3
导航到节点2:
1
4
-> 刷新节点2
2
5
再次刷新node1:
4
6
再次刷新node2:
3
8
这两个页面独立工作,但是应该复制有状态会话 bean 之间的状态。我不明白为什么它不起作用。@SessionScoped bean 之间的状态总是被复制...
我正在寻找一些文档并找到了这个:
在第 8.2 节的末尾有说明:
从 JBoss EAP 7 开始,如果 JBoss EAP 使用 HA 配置文件启动,您的 SFSB 的状态将被复制。
@Stateful bean 是否需要更多配置?
解决方案
上面代码的问题是 @ApplicationScoped SFSB 引用不在 node1 和 node2 之间共享,即使状态本身已被复制。
推荐阅读
- javascript - 页面加载时如何从底部显示消息
- reactjs - 更新 Reactjs PWA 而不关闭它
- c++ - 如何使用迭代器和用户类正确地制作模板功能
- bash - 只是为了在终端更新和升级
- security - 使您的 hasura 数据库配置开源/公开是不好的做法吗?
- c++ - 推力如何确定要传递给函子的参数
- react-native - 当互联网连接不好时,在本机反应中显示错误
- android - 如何在布局中从底部修复按钮?
- java - 如何使用 java 代码而不是 xml 代码将按钮添加到 android studio 中的另一个活动
- python - 使用 GradientTape 时,Tensorflow 渐变总是给出 None