Created
September 22, 2017 20:08
-
-
Save enesakar/e134ba062c26d4634ee0e7fce8ca60ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.hazelcast.test; | |
import com.hazelcast.client.HazelcastClient; | |
import com.hazelcast.client.config.ClientConfig; | |
import com.hazelcast.cluster.ClusterState; | |
import com.hazelcast.core.*; | |
import java.io.Serializable; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Future; | |
public class MainClient { | |
public static void main(String[] args) throws InterruptedException, ExecutionException { | |
final HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(); | |
final HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(); | |
ClientConfig config = new ClientConfig(); | |
HazelcastInstance client = HazelcastClient.newHazelcastClient(config); | |
IExecutorService s = client.getExecutorService("s"); | |
for (int i = 0; i < 1000; i++) { | |
Thread.sleep(1000); | |
Future f = s.submit(new HealthChecker()); | |
System.out.println("state:"+ f.get()); | |
} | |
} | |
static class HealthChecker implements HazelcastInstanceAware, Callable, Serializable { | |
HazelcastInstance instance; | |
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) { | |
instance = hazelcastInstance; | |
} | |
public Boolean call() throws Exception { | |
System.out.println("cluster state:" + instance.getCluster().getClusterState()); | |
// check if cluster is in active and running state | |
if(!instance.getCluster().getClusterState().equals(ClusterState.ACTIVE)) { | |
return false; | |
} | |
System.out.println("is member safe:" + instance.getPartitionService().isLocalMemberSafe()); | |
// check if member is safe, namely all migrations are complete. no data will lost if the member is killed | |
if(!instance.getPartitionService().isLocalMemberSafe()) { | |
return false; | |
} | |
System.out.println("is cluster safe:" + instance.getPartitionService().isClusterSafe()); | |
// check if cluster is safe, namely all migrations are complete cluster-wide. no data will lost if any member is killed | |
if(!instance.getPartitionService().isClusterSafe()) { | |
return false; | |
} | |
// you can also check the cluster size and return false if it is below x | |
int clusterSize = instance.getCluster().getMembers().size(); | |
System.out.println("cluster size:" + clusterSize); | |
// you can also check the client count and return false if it is below x | |
int numberOfClients = instance.getClientService().getConnectedClients().size(); | |
System.out.println("number of Clients:" + numberOfClients); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment