Search in sources :

Example 21 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.

the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened.

@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
    int sleepMsBetweenRetries = 100;
    int maxRetries = 3;
    RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
    try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
        client.start();
        AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
        AtomicBoolean exists = new AtomicBoolean(false);
        async.checkExists().forPath("/").thenAcceptAsync(s -> exists.set(s != null));
        await().until(() -> assertThat(exists.get()).isTrue());
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.Test)

Example 22 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project nakadi by zalando.

the class ZookeeperTestUtils method createCurator.

public static CuratorFramework createCurator(final String zkUrl) {
    final CuratorFramework curator = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new RetryNTimes(5, 500)).sessionTimeoutMs((int) TimeUnit.SECONDS.toMillis(10)).build();
    curator.start();
    return curator;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework)

Example 23 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project drill by apache.

the class TestZkRegistry method connectToZk.

/**
 * Connect to the test ZK for the simulated Drillbit side of the test. (The AM
 * side of the test uses the actual AM code, which is what we're testing
 * here...)
 *
 * @param connectString
 * @return
 */
public static CuratorFramework connectToZk(String connectString) {
    CuratorFramework client = CuratorFrameworkFactory.builder().namespace(ZK_ROOT + "/" + CLUSTER_ID).connectString(connectString).retryPolicy(new RetryNTimes(3, 1000)).build();
    client.start();
    return client;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework)

Example 24 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project drill by apache.

the class TestEphemeralStore method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    ZookeeperTestUtil.setZookeeperSaslTestConfigProps();
    server = new TestingServer();
    final RetryPolicy policy = new RetryNTimes(2, 1000);
    curator = CuratorFrameworkFactory.newClient(server.getConnectString(), policy);
    config = Mockito.mock(TransientStoreConfig.class);
    Mockito.when(config.getName()).thenReturn(root);
    Mockito.when(config.getSerializer()).thenReturn(new InstanceSerializer<String>() {

        @Override
        public byte[] serialize(final String instance) throws IOException {
            if (instance == null) {
                return null;
            }
            return instance.getBytes();
        }

        @Override
        public String deserialize(final byte[] raw) throws IOException {
            if (raw == null) {
                return null;
            }
            return new String(raw);
        }
    });
    store = new ZkEphemeralStore<>(config, curator);
    server.start();
    curator.start();
    store.start();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryNTimes(org.apache.curator.retry.RetryNTimes) TransientStoreConfig(org.apache.drill.exec.coord.store.TransientStoreConfig) IOException(java.io.IOException) RetryPolicy(org.apache.curator.RetryPolicy) Before(org.junit.Before)

Example 25 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project yyl_example by Relucent.

the class CuratorWatcherTest method main.

public static void main(String[] args) throws Exception {
    // 1.Connect to zk
    CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000));
    client.start();
    System.out.println("zk client start successfully!");
    // 2.Register watcher
    PathChildrenCache watcher = new PathChildrenCache(client, ZK_PATH, true);
    watcher.getListenable().addListener((client1, event) -> {
        ChildData data = event.getData();
        if (data == null) {
            System.out.println("No data in event[" + event + "]");
        } else {
            System.out.println(// 
            "Receive event: " + "type=[" + event.getType() + "]" + // 
            ", " + "path=[" + data.getPath() + "]" + // 
            ", " + "data=[" + new String(data.getData()) + "]" + // 
            ", " + "stat=[" + data.getStat() + "]");
        }
    });
    watcher.start(StartMode.BUILD_INITIAL_CACHE);
    System.out.println("Register zk watcher successfully!");
    client.create().creatingParentsIfNeeded().forPath(ZK_CHILDREN_PATH, "hello".getBytes());
    client.setData().forPath(ZK_CHILDREN_PATH, "world".getBytes());
    client.delete().deletingChildrenIfNeeded().forPath(ZK_CHILDREN_PATH);
    watcher.close();
    Thread.sleep(100);
    client.close();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Aggregations

RetryNTimes (org.apache.curator.retry.RetryNTimes)68 CuratorFramework (org.apache.curator.framework.CuratorFramework)51 Test (org.junit.Test)20 RetryPolicy (org.apache.curator.RetryPolicy)12 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)12 Test (org.testng.annotations.Test)11 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 IOException (java.io.IOException)6 ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)5 TestingServer (org.apache.curator.test.TestingServer)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)3 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)3