After installation of hazelcast and adding to Java Build Path, you can write Main.class
that starts cluster work
public static void main(String[] args){
Config config = new Config();
// creates a new HazelcastInstance (a new node in a cluster)
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
// returns the Cluster that this HazelcastInstance is part of
Cluster cluster = instance.getCluster();
// get all devices, that are in the cluster
Set<Member> setMembers = cluster.getMembers();
// get ExecutorService that works on cluster instance
ExecutorService mService = instance.getExecutorService("exec");
for (int i = 0; i < setMembers.size(); i++) {
// send a task for each member on service of HazelcastInstance
final Future<String> future = mService.submit(new ClusterWorkingTask());
String response = null;
try {
// wait for response
response = future.get();
System.out.println(response); // each member return: Hello World!
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
create ClusterWorkingTask.class
that can be executed on each member
public class ClusterWorkingTask implements Callable<String>, Serializable {
@Override
public String call() throws Exception {
// send Hello World! as result of execution
return "Hello World!";
}
}