Example usage for org.apache.zookeeper CreateMode PERSISTENT_WITH_TTL

List of usage examples for org.apache.zookeeper CreateMode PERSISTENT_WITH_TTL

Introduction

In this page you can find the example usage for org.apache.zookeeper CreateMode PERSISTENT_WITH_TTL.

Prototype

CreateMode PERSISTENT_WITH_TTL

To view the source code for org.apache.zookeeper CreateMode PERSISTENT_WITH_TTL.

Click Source Link

Document

The znode will not be automatically deleted upon client's disconnect.

Usage

From source file:org.apache.curator.framework.imps.TestTtlNodes.java

License:Apache License

@Test
public void testBasic() throws Exception {
    try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
            new RetryOneTime(1))) {
        client.start();//from ww  w.  j  a va  2  s.  c  o m

        client.create().withTtl(10).creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_WITH_TTL)
                .forPath("/a/b/c");
        Thread.sleep(20);
        Assert.assertNull(client.checkExists().forPath("/a/b/c"));
    }
}

From source file:org.apache.curator.framework.imps.TestTtlNodes.java

License:Apache License

@Test
public void testBasicInBackground() throws Exception {
    try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
            new RetryOneTime(1))) {
        client.start();//from   w w w.jav  a  2  s. c  o  m

        final CountDownLatch latch = new CountDownLatch(1);
        BackgroundCallback callback = new BackgroundCallback() {
            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                latch.countDown();
            }
        };
        client.create().withTtl(10).creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_WITH_TTL)
                .inBackground(callback).forPath("/a/b/c");
        Assert.assertTrue(new Timing().awaitLatch(latch));
        Thread.sleep(20);
        Assert.assertNull(client.checkExists().forPath("/a/b/c"));
    }
}

From source file:org.apache.curator.framework.recipes.nodes.PersistentTtlNode.java

License:Apache License

/**
 * You must call start() to initiate the persistent ttl node
 *///from  w  ww . j av  a2  s  . c  om
public void start() {
    node.start();

    Runnable touchTask = new Runnable() {
        @Override
        public void run() {
            try {
                try {
                    client.setData().forPath(childPath);
                } catch (KeeperException.NoNodeException e) {
                    client.create().orSetData().withTtl(ttlMs).withMode(CreateMode.PERSISTENT_WITH_TTL)
                            .forPath(childPath);
                }
            } catch (KeeperException.NoNodeException ignore) {
                // ignore
            } catch (Exception e) {
                if (!ThreadUtils.checkInterrupted(e)) {
                    log.debug("Could not touch child node", e);
                }
            }
        }
    };
    Future<?> future = executorService.scheduleAtFixedRate(touchTask, ttlMs / touchScheduleFactor,
            ttlMs / touchScheduleFactor, TimeUnit.MILLISECONDS);
    futureRef.set(future);
}

From source file:pubsub.Clients.java

License:Apache License

private static <T> ModelSpecBuilder<T> builder(Class<T> clazz) {
    return ModelSpec.builder(JacksonModelSerializer.build(clazz)).withTtl(TimeUnit.MINUTES.toMillis(10)) // for our pub-sub example, messages are valid for 10 minutes
            .withCreateMode(CreateMode.PERSISTENT_WITH_TTL);
}