Package com.hazelcast.client.util
Class ClientStateListener
- java.lang.Object
-
- com.hazelcast.client.util.ClientStateListener
-
- All Implemented Interfaces:
LifecycleListener
,java.util.EventListener
public class ClientStateListener extends java.lang.Object implements LifecycleListener
Helper class for the user to track the lifecycle state of the client. The user will instantiate this listener and it will be registered to the client configuration. If the provided client config is not used while instantiating the client, this helper class will not be useful. It is the user's responsibility to instantiate the client with the same ClientConfig which was used to instantiate this helper.
-
-
Constructor Summary
Constructors Constructor Description ClientStateListener(ClientConfig clientConfig)
Registers this instance with the provided client configurationClientStateListener(ClientFailoverConfig clientFailoverConfig)
Registers this instance with the provided client configuration
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitConnected()
Waits until the client is connected to cluster.boolean
awaitConnected(long timeout, java.util.concurrent.TimeUnit unit)
Waits until the client is connected to cluster or the timeout expires.boolean
awaitDisconnected()
Waits until the client is disconnected from the cluster.boolean
awaitDisconnected(long timeout, java.util.concurrent.TimeUnit unit)
Waits until the client is disconnected from the cluster or the timeout expires.LifecycleEvent.LifecycleState
getCurrentState()
boolean
isConnected()
boolean
isShutdown()
boolean
isStarted()
void
stateChanged(LifecycleEvent event)
Called when instance's state changes.
-
-
-
Constructor Detail
-
ClientStateListener
public ClientStateListener(@Nonnull ClientFailoverConfig clientFailoverConfig)
Registers this instance with the provided client configurationThis constructor is introduced to let ClientStateListener to be used via
HazelcastClient.newHazelcastFailoverClient(ClientFailoverConfig)
Listeners used in the different client configs registered to single
ClientFailoverConfig
should be same. It can be achieved using this constructor while the other constructorClientStateListener(ClientConfig)
does not allow that usage.Note that ClientStateListener should be created after all the alternative client configs are added to the client failoverConfig. Example usage:
ClientFailoverConfig clientFailoverConfig = new ClientFailoverConfig(); clientFailoverConfig.addClientConfig(clientConfig).addClientConfig(clientConfig2); ClientStateListener listener = new ClientStateListener(clientFailoverConfig); HazelcastClient.newHazelcastFailoverClient(clientFailoverConfig);
- Parameters:
clientFailoverConfig
- The client configuration to which this listener will be registered- Since:
- 5.0
-
ClientStateListener
public ClientStateListener(@Nonnull ClientConfig clientConfig)
Registers this instance with the provided client configuration- Parameters:
clientConfig
- The client configuration to which this listener will be registered
-
-
Method Detail
-
stateChanged
public void stateChanged(LifecycleEvent event)
Description copied from interface:LifecycleListener
Called when instance's state changes. No blocking calls should be made in this method.- Specified by:
stateChanged
in interfaceLifecycleListener
- Parameters:
event
- the Lifecycle event
-
awaitConnected
public boolean awaitConnected(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Waits until the client is connected to cluster or the timeout expires. Does not wait if the client is already shutting down or shutdown.- Parameters:
timeout
- the maximum time to waitunit
- the time unit of thetimeout
argument- Returns:
- true if the client is connected to the cluster. On returning false,
you can check if timeout occured or the client is shutdown using
isShutdown
getCurrentState
- Throws:
java.lang.InterruptedException
-
awaitConnected
public boolean awaitConnected() throws java.lang.InterruptedException
Waits until the client is connected to cluster. Does not wait if the client is already shutting down or shutdown.- Returns:
- returns whatever
awaitConnected(long timeout, TimeUnit unit)
returns. - Throws:
java.lang.InterruptedException
-
awaitDisconnected
public boolean awaitDisconnected(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Waits until the client is disconnected from the cluster or the timeout expires. Does not wait if the client is already shutting down or shutdown.- Parameters:
timeout
- the maximum time to waitunit
- the time unit of thetimeout
argument- Returns:
- true if the client is disconnected from the cluster. On returning false,
you can check if timeout occured or the client is shutdown using
isShutdown
getCurrentState
- Throws:
java.lang.InterruptedException
-
awaitDisconnected
public boolean awaitDisconnected() throws java.lang.InterruptedException
Waits until the client is disconnected from the cluster. Does not wait if the client is already shutting down or shutdown.- Returns:
- returns whatever
awaitDisconnected(long timeout, TimeUnit unit)
returns. - Throws:
java.lang.InterruptedException
-
isConnected
public boolean isConnected()
- Returns:
- true if the client is connected.
-
isShutdown
public boolean isShutdown()
- Returns:
- true if the client is shutdown.
-
isStarted
public boolean isStarted()
- Returns:
- true if the client is started.
-
getCurrentState
public LifecycleEvent.LifecycleState getCurrentState()
- Returns:
- The current lifecycle state of the client.
-
-