如何停止和关闭整个hazelcast集群?

时间:2014-02-18 23:04:43

标签: java hazelcast

如何停止和关闭淡褐色群集?我对测试的观察是,只要节点被HazelcastInstance#shutdown()停止,集群就会尝试重新平衡或备份数据。如何首先“停止”群集然后将其关闭? (或者我的观察错了吗?)

4 个答案:

答案 0 :(得分:4)

您可以使用isClusterSafe,如下例所示:

public class ShutdownCluster {

public static void main(String[] args) throws Exception {

    HazelcastInstance member1 = Hazelcast.newHazelcastInstance();
    HazelcastInstance member2 = Hazelcast.newHazelcastInstance();
    HazelcastInstance member3 = Hazelcast.newHazelcastInstance();

    if(member1.getPartitionService().isClusterSafe()) {
        IExecutorService executorService = member1.getExecutorService(ShutdownCluster.class.getName());
        executorService.executeOnAllMembers(new ShutdownMember());
    }
}

private static class ShutdownMember implements Runnable, HazelcastInstanceAware, Serializable {

    private HazelcastInstance node;

    @Override
    public void run() {
        node.getLifecycleService().shutdown();
    }

    @Override
    public void setHazelcastInstance(HazelcastInstance node) {
        this.node = node;
    }
}
}

答案 1 :(得分:0)

测试时我经常使用Hazelcast.shutdownAll()

这会杀死所有实例。

答案 2 :(得分:0)

试试这个。

HazelcastInstance.getLifecycleService().shutdown();

答案 3 :(得分:0)

答案永远不会太晚:)

如果您不希望在关机期间触发迁移事件,可以将群集状态更改为PASSIVE:

hazelcastInstance.getCluster().changeClusterState(PASSIVE);

我建议在以下情况下执行此操作:

if (partitionService.isClusterSafe()) {
    hazelcastInstance.getCluster().changeClusterState(PASSIVE);
    hazelcastInstance.getCluster().shutdown(); 
    // or send exit command to each node, as in mrck answer - remember that then action must be an AllowedDuringPassiveState
}

在被动和冻结状态下,Hazelcast不会进行迁移 - 因此 - 在关闭群集时您不会遇到错误。如果阻止有助于确保,那么改变状态和关闭是安全的,以防止数据丢失