本文介绍了在集群环境中,单例是javax.ejb.Singleton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序中为所有用户和群集环境中的所有节点维护一个唯一的计数器。
我想到使用单例会话bean注释javax.ejb.Singleton像这样:

I need to maintain a simple counter that is unique within the application, for all users and all nodes in a clustered environment.I thought about using the singleton session bean annotation javax.ejb.Singleton like so:

package foo;

import javax.ejb.Singleton;

@Singleton
public class Bean {
    private int counter;
    [...]
}

这看起来很简单,但我可以如果在群集环境中按需要工作,则找不到答案。集群的每个节点都有自己的实例吗?

This looks simple, but I could not find an answer if this works as desired in a clustered environment. Would every node of the cluster have it's own instance or not?

当然,我可以将bean保留在数据库中,但它只是一个柜台,这样做会是矫枉过正。此外,我希望计数器在应用程序崩溃或重新启动时重置,所以坚持它会产生比解决问题更多的问题。

Of course I could persist the bean in a database, but it's really only a counter and doing so would be overkill. Also, I want the counter to reset on application crash or restart, so persisting it would create more problems than it solves.

推荐答案

是的,每个群集节点都将一个不同的Singleton实例。因此,@Singleton注释不是您的问题的解决方案。

Yes, each cluster node will have a different Singleton instance. Therefore, @Singleton annotation is not the solution for your problem.

请记住,Java EE规范没有定义任何类型的集群行为。您需要搜索具体的供应商解决方案才能实现这种需求。例如,请参阅。

Take in mind that Java EE specification doesn't define any kind of cluster behavior. You need to search for specific vendor solution to achieve this kind of requirement. Just as an example see this link.

这篇关于在集群环境中,单例是javax.ejb.Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:14