java – 使用Wildfly的Clustered Singleton?

我正在尝试在Wildfly 8.2上创建一个简单的集群Singleton.我已经配置了2个Wildfly实例,在独立的集群配置中运行.我的应用程序已部署到两者,我可以毫无问题地访问它.

我的集群EJB看起来像这样:

@Named
@Clustered
@Singleton
public class PeekPokeEJB implements PeekPoke {

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger
            .getLogger(PeekPokeEJB.class);

    private static final long serialVersionUID = 2332663907180293111L;

    private int value = -1;

    @Override
    public void poke() {
        if (logger.isDebugEnabled()) {
            logger.debug("poke() - start"); //$NON-NLS-1$
        }

        Random rand = new SecureRandom();
        int newValue = rand.nextInt();
        if (logger.isDebugEnabled()) {
            logger.debug("poke() - int newValue=" + newValue); //$NON-NLS-1$
        }

        this.value = newValue;

        if (logger.isDebugEnabled()) {
            logger.debug("poke() - end"); //$NON-NLS-1$
        }
    }

    @Override
    public void peek() {
        if (logger.isDebugEnabled()) {
            logger.debug("peek() - start"); //$NON-NLS-1$
        }

        if (logger.isDebugEnabled()) {
            logger.debug("peek() - value=" + value); //$NON-NLS-1$
        }

        if (logger.isDebugEnabled()) {
            logger.debug("peek() - end"); //$NON-NLS-1$
        }
    }
}

…我写了一个非常简单的RESTful服务,让我通过浏览器调用这些方法……

@Path("/test")
@Named
public class TestRS extends AbstractRestService {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(TestRS.class);

    @Inject
    private PeekPoke ejb = null;

    @GET
    @Path("/poke")
    public void poke() {
        if (logger.isDebugEnabled()) {
            logger.debug("poke() - start"); //$NON-NLS-1$
        }

        this.ejb.poke();

        if (logger.isDebugEnabled()) {
            logger.debug("poke() - end"); //$NON-NLS-1$
        }
    }

    @GET
    @Path("/peek")
    public void peek() {
        if (logger.isDebugEnabled()) {
            logger.debug("peek() - start"); //$NON-NLS-1$
        }

        this.ejb.peek();

        if (logger.isDebugEnabled()) {
            logger.debug("peek() - end"); //$NON-NLS-1$
        }
    }
}

我可以从单个Wildfly实例中调用peek和poke方法并获得预期值.但是,如果我试图从一个实例调用poke,并从另一个实例查看,我发现这些值没有在EJB之间复制.

我的印象是,群集单例会在两个应用程序服务器上复制’value’的值,无论我从哪个主机调用peek,都会提供相同的值.这不正确吗?是否还有一些我需要添加到此代码中的内容?

我很感激你能给我的任何帮助!谢谢!

Singleton会话bean提供了一种正式的编程结构,可以保证会话bean在特定Java虚拟机(JVM)中的每个应用程序中实例化一次.

JSR 318:Enterprise JavaBeans TM 3.1版说:

A Singleton session bean is a session bean component that is
instantiated once per application. In cases where the container is
distributed over many virtual machines, each application will have one
bean instance of the Singleton for each JVM

因此,在集群应用程序中,每个集群成员都将拥有自己的单例会话bean实例,并且不会跨JVM实例共享数据(在Wildfly实现中).

在Wildfly中,如果在集群范围中只需要一个单例实例,则可以使用SingletonService实现.使用SingletonService,目标服务安装在集群中的每个节点上,但仅在任何给定时间在一个节点上启动.

看到:

> Implement an HA Singleton
> cluster-ha-singleton: A SingletonService deployed in a JAR started
by SingletonStartup and accessed by an EJB

更新:

WildFly 10 adds the ability to deploy a given application as a
“singleton deployment”. This is a new implementation of a feature that
existed in AS 6.0 and earlier. When deployed to a group of clustered
servers, a singleton deployment will only deploy on a single node at
any given time. If the node on which the deployment is active stops or
fails, the deployment will automatically start on another node.

见:WildFly 10 Final is now available!

https://stackoverflow.com/questions/27952769/clustered-singleton-using-wildfly

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 使用Wildfly的Clustered Singleton?