| Table of Contents |
|---|
...
| Code Block | ||
|---|---|---|
| ||
public class ClusterSample {
public static void main(String[] args) {
ClusterManager clusterManager = new HazelcastClusterManager("FEJNode1"); //(1)
clusterManager.addLocalNodeLeaderListener(new LocalNodeLeaderListenerAdaptor() {
@Override
public void onGranted() { //(2)
System.out.println("This node was elected as a leader");
}
@Override
public void onRevoked() { //(3)
System.out.println("This node isn't leader any more");
}
});
clusterManager.join(); //(4)
clusterManager.electLeader(clusterManager.localNode().id()); //(5)
//pause 1 sec
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
//print nodes
clusterManager.nodes().stream().forEach(System.out::println); //(6)
clusterManager.leave(); //(7)
}
} |
...
If the quorum size is not specified, it is considered that even one node already composes a quorum.
| Info |
|---|
Current implementation only automatically chooses a leader in the cluster when the cluster did not have a leader before. |
The first time leader selection algorithm is:
If the new node has decided that it is necessary to choose a leader, it sends the
LEADER_EVENTevent to the cluster and offers a new leader.Once the proposed new leader gets the
LEADER_EVENTevent with its ID, it notifies the application about its new status by callingLocalNodeLeaderListener.onGranted()and sends theLEADER_STARTED_EVENTevent with its ID back to the cluster.When the rest of the cluster nodes receive the
LEADER_STARTED_EVENTevent , they set their state to BACKUP (mark themselves as backup), and notify their applications about the new status by calling LocalNodeBackupListener.onBackup().
| Info |
|---|
You can check the presence of the leader in the cluster by using |
Appointment of a new leader
...