Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

...

Code Block
languagejava
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:

  1. If the new node has decided that it is necessary to choose a leader, it sends the LEADER_EVENT event to the cluster and offers a new leader.

  2. Once the proposed new leader gets the LEADER_EVENT event with its ID, it notifies the application about its new status by calling LocalNodeLeaderListener.onGranted() and sends the LEADER_STARTED_EVENT event with its ID back to the cluster.

  3. When the rest of the cluster nodes receive the LEADER_STARTED_EVENT event , 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 HazelcastClusterManager.hasLeader() method.

Appointment of a new leader

...