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

319 views
Skip to first unread message

erez....@credorax.com

unread,
Feb 6, 2020, 4:40:55 AM2/6/20
to Hazelcast
Hi
from looking in the group and in the StackOverflow thread I found that the solution is to get any kind of collections from the cluster, to check if the collection is not null and then perform an action (put, size or any other method that update the collection) in case the cluster is down an exception will be thrown.

from debugging the code I found that there is a dedicated class ClusterConnectorService which have a boolean method  isClusterAvailable();
is there any reason why the HazelcastInstance doesn't have access to the class and the method?

are they any risks using the following code?
HazelcastClientProxy clientProxy = (HazelcastClientProxy) hazelcastInstance;
clusterConnectorService = clientProxy.client.getClusterConnectorService();
if (!clusterConnectorService.isClusterAvailable()){
 // mark there are no cluster aviable
 }

M. Sancar Koyunlu

unread,
Feb 6, 2020, 8:08:15 AM2/6/20
to Hazelcast
Hi,
It is an internal method and subjected to change, I won't recommend to use it. 

For your use case, I recommend LifecycleService. From there, you can check if your client is connected the cluster or not via a listener.
Note that, this is still not answering if cluster is alive or not. It still be the case that cluster is there but your connection is broken for any other reason.
CLIENT_CONNECTED and CLIENT_DISCONNECTED is enough for your use case. Other events will also be thrown for the client, but in your particular case, you can ignore them. 


HazelcastInstance client = HazelcastClient.newHazelcastClient();
client.getLifecycleService().addLifecycleListener(
new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
//handle
CLIENT_CONNECTED , CLIENT_DISCONNECTED
}
});
Alternatively we have a utility implementation to check the current state as follows:
ClientConfig config = new ClientConfig();
ClientStateListener clientStateListener = new ClientStateListener(config);
HazelcastInstance client = HazelcastClient.newHazelcastClient(config);
System.out.println(clientStateListener.isConnected());
Also note that following method can return true even if client is not connected. Client will try to connect back to cluster for some time(according to user configuration), and eventually be shutdown. This return false only when client is shutdown.

HazelcastInstance hzInstance = HazelcastClient.newHazelcastClient();
hzInstance.getLifecycleService().isRunning()
Regards

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/e76f4fae-04a7-4d27-b794-4fedb23a7475%40googlegroups.com.


--
M. Sancar Koyunlu
Software Engineer, 
Hazelcast

This message contains confidential information and is intended only for the individuals named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required, please request a hard-copy version. -Hazelcast
Reply all
Reply to author
Forward
0 new messages