Search in sources :

Example 11 with CreateBuilder

use of org.apache.curator.framework.api.CreateBuilder in project pravega by pravega.

the class ZKStoreHelper method createZNodeIfNotExist.

CompletableFuture<Void> createZNodeIfNotExist(final String path, final byte[] data, final boolean createParent) {
    final CompletableFuture<Void> result = new CompletableFuture<>();
    try {
        CreateBuilder createBuilder = client.create();
        BackgroundCallback callback = callback(x -> result.complete(null), e -> {
            if (e instanceof StoreException.DataExistsException) {
                result.complete(null);
            } else {
                result.completeExceptionally(e);
            }
        }, path);
        if (createParent) {
            createBuilder.creatingParentsIfNeeded().inBackground(callback, executor).forPath(path, data);
        } else {
            createBuilder.inBackground(callback, executor).forPath(path, data);
        }
    } catch (Exception e) {
        result.completeExceptionally(StoreException.create(StoreException.Type.UNKNOWN, e, path));
    }
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

CreateBuilder (org.apache.curator.framework.api.CreateBuilder)11 KeeperException (org.apache.zookeeper.KeeperException)5 CompletableFuture (java.util.concurrent.CompletableFuture)3 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)3 ExistsBuilder (org.apache.curator.framework.api.ExistsBuilder)3 Test (org.testng.annotations.Test)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 SetDataBuilder (org.apache.curator.framework.api.SetDataBuilder)2 CreateMode (org.apache.zookeeper.CreateMode)2 ACL (org.apache.zookeeper.data.ACL)2 Id (org.apache.zookeeper.data.Id)2 Test (org.junit.Test)2 BeforeTest (org.testng.annotations.BeforeTest)2 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 SetupStep (org.apache.atlas.setup.SetupStep)1 ACLBackgroundPathAndBytesable (org.apache.curator.framework.api.ACLBackgroundPathAndBytesable)1 DeleteBuilder (org.apache.curator.framework.api.DeleteBuilder)1