Search in sources :

Example 51 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project microservices by pwillhan.

the class Zookeeper method register.

public void register() {
    CuratorFramework curator = CuratorFrameworkFactory.newClient(host + ":" + port, new RetryNTimes(3, 3000));
    curator.start();
    try {
        final ServiceInstance<Object> serviceInstance = ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}")).address(getIp()).port(getPort()).name("geolocation").build();
        final ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class).basePath("com.packt.microservices").client(curator).thisInstance(serviceInstance).build();
        serviceDiscovery.start();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                serviceDiscovery.unregisterService(serviceInstance);
            } catch (Exception e) {
                System.err.println("Error while unregistering service in Zookeeper. Details: " + e.getMessage());
                e.printStackTrace();
            }
        }));
    } catch (Exception e) {
        System.err.println("Error while registering service in Zookeeper. Details: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) UriSpec(org.apache.curator.x.discovery.UriSpec) UnknownHostException(java.net.UnknownHostException)

Example 52 with RetryNTimes

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

the class ZKStoreHelperTest method testEphemeralNode.

@Test(timeout = 30000)
public void testEphemeralNode() {
    CuratorFramework cli2 = CuratorFrameworkFactory.newClient(zkServer.getConnectString(), new RetryNTimes(0, 0));
    cli2.start();
    ZKStoreHelper zkStoreHelper2 = new ZKStoreHelper(cli2, executor);
    assertTrue(zkStoreHelper2.createEphemeralZNode("/testEphemeral", new byte[0]).join());
    Assert.assertNotNull(zkStoreHelper2.getData("/testEphemeral", x -> x).join());
    zkStoreHelper2.getClient().getZookeeperClient().close();
    zkStoreHelper2.getClient().close();
    // let session get expired.
    // now read the data again. Verify that node no longer exists
    AssertExtensions.assertFutureThrows("", Futures.delayedFuture(() -> zkStoreHelper.getData("/testEphemeral", x -> x), 1000, executor), e -> e instanceof StoreException.DataNotFoundException);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) StoreException(io.pravega.controller.store.stream.StoreException) Test(org.junit.Test)

Example 53 with RetryNTimes

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

the class ZkOrderedStoreTest method setup.

@Before
public void setup() throws Exception {
    zkServer = new TestingServerStarter().start();
    cli = CuratorFrameworkFactory.newClient(zkServer.getConnectString(), 10000, 10000, new RetryNTimes(0, 0));
    cli.start();
    zkStoreHelper = new ZKStoreHelper(cli, executor);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) ZKStoreHelper(io.pravega.controller.store.ZKStoreHelper) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Before(org.junit.Before)

Example 54 with RetryNTimes

use of org.apache.curator.retry.RetryNTimes in project Saturn by vipshop.

the class NestedZkUtils method createClient.

public CuratorFramework createClient(String namespace) throws InterruptedException {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework curatorFramework = builder.connectString("127.0.0.1:" + PORT).sessionTimeoutMs(// long long, could to debug
    600 * 1000).retryPolicy(new RetryNTimes(3, 1000)).namespace(namespace).build();
    curatorFramework.start();
    curatorFramework.blockUntilConnected();
    return curatorFramework;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory)

Example 55 with RetryNTimes

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

the class CuratorClientTest 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.Client API test
    // 2.1 Create node
    String data1 = "hello";
    print("create", ZK_PATH, data1);
    client.create().creatingParentsIfNeeded().forPath(ZK_PATH, data1.getBytes());
    // 2.2 Get node and data
    print("ls", "/");
    print(client.getChildren().forPath("/"));
    print("get", ZK_PATH);
    print(client.getData().forPath(ZK_PATH));
    // 2.3 Modify data
    String data2 = "world";
    print("set", ZK_PATH, data2);
    client.setData().forPath(ZK_PATH, data2.getBytes());
    print("get", ZK_PATH);
    print(client.getData().forPath(ZK_PATH));
    // 2.4 Remove node
    print("delete", ZK_PATH);
    client.delete().forPath(ZK_PATH);
    print("ls", "/");
    print(client.getChildren().forPath("/"));
    client.close();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework)

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