如何为多个jvm支持创建单例java类? [英] How to create singleton java class for multiple jvm support?

查看:172
本文介绍了如何为多个jvm支持创建单例java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有DBManager.java Singleton Class,我必须在集群环境中部署它。
这是一个基于Web的应用程序,具有以下部署策略

For example I have DBManager.java Singleton Class, which I have to deploy on clustered environment. It is a web based application, with following deployment stratergy

Apache负载均衡器 - > Tomcat 6(集群中的3个服务器)。

Apache Load Balancer --> Tomcat 6 (3 Servers in cluster).

我必须为3个tomcat实例维护DBManager的单个实例。

I have to maintain single instance of DBManager for 3 tomcat instances.

我的代码是

package com.db.util;
public class DBManager {
    private static DBManager singleInstance;
    private DBManager () {}
    public static DBManager getSingleInstance() {
        if (singleInstance == null) {
            synchronized (DBManager.class) {
                if (singleInstance == null) {
                    singleInstance = new DBManager ();
                }
            }
        }
        return singleInstance;
    }
}

我一直在寻找这个问题的解决方案,发现类似JGroups API的东西。
这可以使用JGroups实现吗?任何想法,如何实现?

I have been searching a solution to this problem, and found something like JGroups API. Can this be achieved using JGroups ? Any Idea, How to implement that ?

推荐答案

Java在每个实例中给你一个单例,你需要在它之间进行某种协调实例所以在任何给定时间其中一个是活动的,但如果活动的一个死亡,则另一个实例变为活动状态。

Java gives you a singleton in each instance, you need some kind of coordination between the instances so at any given time one of them is active, but if the active one dies then a different instance becomes active.

一些应用服务器具有内置的功能来控制这样的协调工作者实例,我不知道Tomcat是否具有这样的功能。

Some app servers have built in capabilities to control such coordinated worker instances, I don't know whether Tomcat has such a function.

自己构建这样的功能非常困难,请参阅这个问题并注意到这个问题提供了一个有用的库的链接 - 对我来说这看起来很复杂。

Building such functionality yourself is surprisingly difficult, see this question and note that that question gives links to a useful library - which to me looks quite complex to use.

但是在你的情况下,你有一个数据库,这给你一个协调点。我没有详细设计这个,但我认为可以使用控制表中的专用行创建预订方案。有效地执行此操作有点棘手,平衡实例死亡的检测速度与轮询数据库的开销以查看哪个实例处于活动状态,但似乎可行。

However in your case you have a database, and that gives you a point of coordination. I haven't designed this in detail, but I reckon it's possible to create a reservation scheme using a dedicated row in a control table. It will be a bit tricky to do this efficiently, balancing the speed of detection of an instance death with the overheads of polling the database to see which instance is active, but it seems doable.

这个想法是该记录包含一个reservedUntil时间戳和processId。每个进程读取记录,如果它包含它自己的id并且时间戳尚未到期,则它知道它可以工作。当时间接近到期时,活动进程使用乐观锁定样式Update where timestamp == old timestamp来更新时间戳以管理竞争条件。每个非活动进程等待,直到它上次读取的时间戳已过期,然后尝试通过更新记录来控制,再次使用乐观锁定Update where。通常,控制的尝试将失败,但如果成功,我们现在有一个新的活动实例,并且由于乐观锁定,我们只能获得一个活动实例。

The idea is that the record contains a "reservedUntil" timestamp and "processId". Each process reads the record, if it contains it's own id and the timestamp has not yet expired it knows it can work. When the time is nearly expired, the active process updates the timestamp using an optimistic locking style "Update where timestamp == old timestamp" to manage race conditions. Each non active process waits until the timestamp it last read has expired and then attempts to to take control by updating the record, again using an optimistic locking Update where. Usually that attempt to take control will fail, but if it succeeds we now have a new active instance, and due to optimistic locking we can only ever get one active instance.

这篇关于如何为多个jvm支持创建单例java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆