首页 > 解决方案 > 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 之间的状态总是被复制...

我正在寻找一些文档并找到了这个:

https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html/developing_ejb_applications/clustered_enterprise_javabeans#cluster_topology_communication

在第 8.2 节的末尾有说明:

从 JBoss EAP 7 开始,如果 JBoss EAP 使用 HA 配置文件启动,您的 SFSB 的状态将被复制。

@Stateful bean 是否需要更多配置?

标签: infinispanjboss-eap-7stateful-session-beanejb-3.2

解决方案


上面代码的问题是 @ApplicationScoped SFSB 引用不在 node1 和 node2 之间共享,即使状态本身已被复制。


推荐阅读