Search in sources :

Example 36 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project chassis by Kixeye.

the class CuratorFrameworkBuilder method buildCuratorWithExhibitor.

private CuratorFramework buildCuratorWithExhibitor(Configuration configuration) {
    LOGGER.debug("configuring zookeeper connection through Exhibitor...");
    ExhibitorEnsembleProvider ensembleProvider = new KixeyeExhibitorEnsembleProvider(exhibitors, new KixeyeExhibitorRestClient(configuration.getBoolean(EXHIBITOR_USE_HTTPS.getPropertyName())), configuration.getString(EXHIBITOR_URI_PATH.getPropertyName()), configuration.getInt(EXHIBITOR_POLL_INTERVAL.getPropertyName()), new ExponentialBackoffRetry(configuration.getInt(EXHIBITOR_INITIAL_SLEEP_MILLIS.getPropertyName()), configuration.getInt(EXHIBITOR_MAX_RETRIES.getPropertyName()), configuration.getInt(EXHIBITOR_RETRIES_MAX_MILLIS.getPropertyName())));
    // ensures that the SERVER list from Exhibitor is already downloaded before curator attempts to connect to zookeeper.
    try {
        ensembleProvider.pollForInitialEnsemble();
    } catch (Exception e) {
        try {
            Closeables.close(ensembleProvider, true);
        } catch (IOException e1) {
        }
        throw new BootstrapException("Failed to initialize Exhibitor with host(s) " + exhibitors.getHostnames(), e);
    }
    CuratorFramework curator = CuratorFrameworkFactory.builder().ensembleProvider(ensembleProvider).retryPolicy(buildZookeeperRetryPolicy(configuration)).build();
    curator.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            LOGGER.debug("Connection state to ZooKeeper changed: " + newState);
        }
    });
    return curator;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) BootstrapException(com.kixeye.chassis.bootstrap.BootstrapException) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) IOException(java.io.IOException) ConnectionState(org.apache.curator.framework.state.ConnectionState) BootstrapException(com.kixeye.chassis.bootstrap.BootstrapException) IOException(java.io.IOException) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Example 37 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project chassis by Kixeye.

the class CuratorFrameworkBuilder method buildCuratorWithZookeeperDirectly.

private CuratorFramework buildCuratorWithZookeeperDirectly(Configuration configuration) {
    LOGGER.debug("configuring direct zookeeper connection.");
    CuratorFramework curator = CuratorFrameworkFactory.newClient(this.zookeeperConnectionString, configuration.getInt(ZOOKEEPER_SESSION_TIMEOUT_MILLIS.getPropertyName()), configuration.getInt(ZOOKEEPER_CONNECTION_TIMEOUT_MILLIS.getPropertyName()), buildZookeeperRetryPolicy(configuration));
    curator.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            LOGGER.debug("Connection state to ZooKeeper changed: " + newState);
        }
    });
    return curator;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)37 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)37 ConnectionState (org.apache.curator.framework.state.ConnectionState)36 Test (org.testng.annotations.Test)29 Timing (org.apache.curator.test.Timing)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 RetryOneTime (org.apache.curator.retry.RetryOneTime)20 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)9 KeeperException (org.apache.zookeeper.KeeperException)9 TestingCluster (org.apache.curator.test.TestingCluster)5 TestingServer (org.apache.curator.test.TestingServer)5 IOException (java.io.IOException)4 Semaphore (java.util.concurrent.Semaphore)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 InstanceSpec (org.apache.curator.test.InstanceSpec)4 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)3 ExecutorService (java.util.concurrent.ExecutorService)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2