Search code examples
javaclient-serverhazelcast

How can I check if a hazelcast cluster is alive from a java client?


We use hazelcast in client-server mode. The hazelcast cluster contains 2 hazelcast nodes and we have about 25 clients connected to the cluster.

What I am lookin for now is a simple check that tries to figure out if the cluster is still alive. It should be a rather cheap operation because this check will occure on every client quite frequently (once every second I could imagine).

What is the best way to do so?


Solution

  • The simplest way would be the register a LifecycleListener to the client HazelcastInstance:

        HazelcastInstance client = HazelcastClient.newHazelcastClient();
        client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
            @Override
            public void stateChanged(LifecycleEvent event) {
    
            }
        })
    

    The client uses a periodic heartbeat to detect if the cluster is still running.