com.hazelcast.core.Cluster.changeClusterState()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中com.hazelcast.core.Cluster.changeClusterState()方法的一些代码示例,展示了Cluster.changeClusterState()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cluster.changeClusterState()方法的具体详情如下:
包路径:com.hazelcast.core.Cluster
类名称:Cluster
方法名:changeClusterState

Cluster.changeClusterState介绍

[英]Changes state of the cluster to the given state transactionally. Transaction will be TWO_PHASE and will have 1 durability by default. If you want to override transaction options, use #changeClusterState(ClusterState,TransactionOptions).

If the given state is already same as current state of the cluster, then this method will have no effect.

If there's an ongoing state change transaction in the cluster, this method will fail immediately with a TransactionException.

If a membership change occurs in the cluster during state change, a new member joins or an existing member leaves, then this method will fail with an IllegalStateException.

If there are ongoing/pending migration/replication operations, because of re-balancing due to member join or leave, then trying to change from ACTIVE to FROZENor PASSIVE will fail with an IllegalStateException.

If transaction timeouts during state change, then this method will fail with a TransactionException.
[中]以事务方式将集群的状态更改为给定状态。事务将是两个阶段,默认情况下具有1个持久性。如果要覆盖事务选项,请使用#changeClusterState(ClusterState,TransactionOptions)。
如果给定状态已经与集群的当前状态相同,则此方法将无效。
如果集群中有正在进行的状态更改事务,则此方法将立即失败,并出现TransactionException。
如果在状态更改期间集群中发生成员身份更改,新成员加入或现有成员离开,则此方法将失败,并出现IllegalStateException。
如果存在正在进行/挂起的迁移/复制操作(由于成员加入或离开而重新平衡),则尝试从主动更改为冻结或被动将失败,并出现IllegalStateException。
如果在状态更改期间事务超时,则此方法将因TransactionException而失败。

代码示例

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    System.setProperty("hazelcast.phone.home.enabled", "false");

    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();

    IMap<Object, Object> map = instance2.getMap("test-map");
    // initialize partition assignments
    map.size();

    System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
    System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

    instance2.getCluster().changeClusterState(ClusterState.NO_MIGRATION);
    System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
    System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

    // start a new instance
    HazelcastInstance instance3 = Hazelcast.newHazelcastInstance();

    System.out.println("Instance-3 Members: " + instance3.getCluster().getMembers());
    System.out.println("Instance-3 Cluster State: " + instance3.getCluster().getClusterState());

    Hazelcast.shutdownAll();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());
instance2.getCluster().changeClusterState(ClusterState.FROZEN);
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) throws InterruptedException {
  IOUtil.delete(new File(HOT_RESTART_ROOT_DIR + "5701"));
  IOUtil.delete(new File(HOT_RESTART_ROOT_DIR + "5702"));
  HazelcastInstance instance1 = newHazelcastInstance(5701);
  HazelcastInstance instance2 = newHazelcastInstance(5702);
  Cache<Integer, String> cache = createCache(instance2);
  for (int i = 0; i < 50; i++) {
    cache.put(i, "value" + i);
  }
  instance2.getCluster().changeClusterState(ClusterState.PASSIVE);
  instance1.shutdown();
  Thread.sleep(5000L);
  instance1 = newHazelcastInstance(5701);
  instance1.getCluster().changeClusterState(ClusterState.ACTIVE);
  for (int i = 0; i < 50; i++) {
    System.out.println("cache.get(" + i + ") = " + cache.get(i));
  }
  Hazelcast.shutdownAll();
}

代码示例来源:origin: hazelcast/hazelcast-jet

@Override
public void writeResponse(ManagementCenterService mcs, JsonObject out) throws Exception {
  String resultString = "SUCCESS";
  try {
    Cluster cluster = mcs.getHazelcastInstance().getCluster();
    cluster.changeClusterState(getClusterState(state));
  } catch (Exception e) {
    ILogger logger = mcs.getHazelcastInstance().node.nodeEngine.getLogger(getClass());
    logger.warning("Cluster state can not be changed: ", e);
    resultString = FAILURE + e.getMessage();
  }
  JsonObject result = new JsonObject().add("result", resultString);
  out.add("result", result);
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

instance2.getCluster().changeClusterState(ClusterState.PASSIVE);
System.out.println("Instance-1 Cluster State: " + instance1.getCluster().getClusterState());
System.out.println("Instance-2 Cluster State: " + instance2.getCluster().getClusterState());

代码示例来源:origin: com.hazelcast/hazelcast-all

@Override
public void writeResponse(ManagementCenterService mcs, JsonObject out) throws Exception {
  String resultString = "SUCCESS";
  try {
    Cluster cluster = mcs.getHazelcastInstance().getCluster();
    cluster.changeClusterState(getClusterState(state));
  } catch (Exception e) {
    ILogger logger = mcs.getHazelcastInstance().node.nodeEngine.getLogger(getClass());
    logger.warning("Cluster state can not be changed: ", e);
    resultString = FAILURE + e.getMessage();
  }
  JsonObject result = new JsonObject().add("result", resultString);
  out.add("result", result);
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
  IOUtil.delete(new File(HOT_RESTART_ROOT_DIR + "5701"));
  IOUtil.delete(new File(HOT_RESTART_ROOT_DIR + "5702"));
  HazelcastInstance instance1 = newHazelcastInstance(5701);
  HazelcastInstance instance2 = newHazelcastInstance(5702);
  Cache<Integer, String> cache = createCache(instance1);
  for (int i = 0; i < 50; i++) {
    cache.put(i, "value" + i);
  }
  instance2.getCluster().shutdown();
  // Offloading to a thread.
  // Because all instances should start in parallel
  // to be able to do hot-restart cluster verification
  new Thread() {
    public void run() {
      newHazelcastInstance(5701);
    }
  }.start();
  instance2 = newHazelcastInstance(5702);
  instance2.getCluster().changeClusterState(ClusterState.ACTIVE);
  cache = createCache(instance2);
  for (int i = 0; i < 50; i++) {
    System.out.println("cache.get(" + i + ") = " + cache.get(i));
  }
  Hazelcast.shutdownAll();
}

相关文章