Skip to content

Instantly share code, notes, and snippets.

@definitelyMVP
Last active November 5, 2019 15:12
Show Gist options
  • Save definitelyMVP/4983940 to your computer and use it in GitHub Desktop.
Save definitelyMVP/4983940 to your computer and use it in GitHub Desktop.
a curator ConnectionStateListener example
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.framework.state.ConnectionState;
import com.netflix.curator.framework.state.ConnectionStateListener;
import com.netflix.curator.retry.ExponentialBackoffRetry;
/**
* 1.run this, you will see output "** STATE CHANGED TO : CONNECTED";
* 2.kill your zk server, you will see output "** STATE CHANGED TO : SUSPENDED";
* 3.start your zk server, you will see output "** STATE CHANGED TO : RECONNECTED".
*
* @author yl
* @date 2013-02-19
*/
public class ConnectionStateListenerExample {
public static void main(String[] args) throws Exception {
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, Integer.MAX_VALUE);
CuratorFramework curator = CuratorFrameworkFactory.newClient("10.12.136.235:2181", retryPolicy);
curator.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
System.out.println("** STATE CHANGED TO : " + newState);
}
});
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
Thread.sleep(Integer.MAX_VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment