Class AsyncWrappers
- java.lang.Object
-
- org.apache.curator.x.async.AsyncWrappers
-
public class AsyncWrappers extends java.lang.ObjectUtility for adding asynchronous behavior
E.g. locks:
InterProcessMutex mutex = new InterProcessMutex(...) // or any InterProcessLock AsyncWrappers.lockAsync(mutex, executor).thenAccept(dummy -> { try { // do work while holding the lock } finally { AsyncWrappers.release(mutex); } }).exceptionally(e -> { if ( e instanceOf TimeoutException ) { // timed out trying to acquire the lock } // handle the error return null; });E.g. EnsureContainers
AsyncWrappers.(client, path, executor).thenAccept(dummy -> { // execute after ensuring containers });
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAsyncWrappers.TimeoutExceptionSet as the completion stage's exception when trying to acquire a lock times out
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.concurrent.CompletionStage<java.lang.Void>asyncEnsureContainers(AsyncCuratorFramework client, java.lang.String path)Asynchronously ensure that the parents of the given path are created as containersstatic java.util.concurrent.CompletionStage<java.lang.Void>asyncEnsureParents(AsyncCuratorFramework client, java.lang.String path)Asynchronously ensure that the parents of the given path are createdstatic java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,byte[]>>childrenWithData(AsyncCuratorFramework client, java.lang.String path)Return the children of the given path (keyed by the full path) and the data for each node.static java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,byte[]>>childrenWithData(AsyncCuratorFramework client, java.lang.String path, boolean isCompressed)Return the children of the given path (keyed by the full path) and the data for each node.static java.util.concurrent.CompletionStage<java.lang.Void>lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock)Attempt to acquire the given lock asynchronously without timeout using theForkJoinPool.commonPool().static java.util.concurrent.CompletionStage<java.lang.Void>lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit)Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool().static java.util.concurrent.CompletionStage<java.lang.Void>lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given timeout and executor.static java.util.concurrent.CompletionStage<java.lang.Void>lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given executor and without a timeout.static java.util.concurrent.CompletionStage<java.lang.Boolean>lockAsyncIf(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit)Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool().static java.util.concurrent.CompletionStage<java.lang.Boolean>lockAsyncIf(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given timeout and executor.static voidrelease(org.apache.curator.framework.recipes.locks.InterProcessLock lock)Release the lock and wrap any exception inRuntimeExceptionstatic voidrelease(org.apache.curator.framework.recipes.locks.InterProcessLock lock, boolean ignoreNoLockExceptions)Release the lock and wrap any exception inRuntimeException
-
-
-
Method Detail
-
childrenWithData
public static java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,byte[]>> childrenWithData(AsyncCuratorFramework client, java.lang.String path)
Return the children of the given path (keyed by the full path) and the data for each node. IMPORTANT: this results in a ZooKeeper query for each child node returned. i.e. if the initial children() call returns 10 nodes an additional 10 ZooKeeper queries are made to get the data.
Note: if the any of the nodes in the path do not exist yet,
KeeperException.NoNodeExceptionis NOT set. Instead the stage is completed with an empty map.- Returns:
- CompletionStage
-
childrenWithData
public static java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,byte[]>> childrenWithData(AsyncCuratorFramework client, java.lang.String path, boolean isCompressed)
Return the children of the given path (keyed by the full path) and the data for each node. IMPORTANT: this results in a ZooKeeper query for each child node returned. i.e. if the initial children() call returns 10 nodes an additional 10 ZooKeeper queries are made to get the data.
Note: if the any of the nodes in the path do not exist yet,
KeeperException.NoNodeExceptionis NOT set. Instead the stage is completed with an empty map.- Parameters:
isCompressed- pass true if data is compressed- Returns:
- CompletionStage
-
asyncEnsureParents
public static java.util.concurrent.CompletionStage<java.lang.Void> asyncEnsureParents(AsyncCuratorFramework client, java.lang.String path)
Asynchronously ensure that the parents of the given path are created- Parameters:
client- clientpath- path to ensure- Returns:
- stage
-
asyncEnsureContainers
public static java.util.concurrent.CompletionStage<java.lang.Void> asyncEnsureContainers(AsyncCuratorFramework client, java.lang.String path)
Asynchronously ensure that the parents of the given path are created as containers- Parameters:
client- clientpath- path to ensure- Returns:
- stage
-
lockAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given timeout and executor. If the lock is not acquired within the timeout stage is completedExceptionally withAsyncWrappers.TimeoutException- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)timeout- max timeout to acquire lockunit- time unit of timeoutexecutor- executor to use to asynchronously acquire- Returns:
- stage
-
lockAsyncIf
public static java.util.concurrent.CompletionStage<java.lang.Boolean> lockAsyncIf(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given timeout and executor. The stage is completed with a Boolean that indicates whether or not the lock was acquired.- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)timeout- max timeout to acquire lockunit- time unit of timeoutexecutor- executor to use to asynchronously acquire- Returns:
- stage
-
lockAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, java.util.concurrent.Executor executor)Attempt to acquire the given lock asynchronously using the given executor and without a timeout.- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)executor- executor to use to asynchronously acquire- Returns:
- stage
-
lockAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit)Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool(). If the lock is not acquired within the timeout stage is completedExceptionally withAsyncWrappers.TimeoutException- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)timeout- max timeout to acquire lockunit- time unit of timeout- Returns:
- stage
-
lockAsyncIf
public static java.util.concurrent.CompletionStage<java.lang.Boolean> lockAsyncIf(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long timeout, java.util.concurrent.TimeUnit unit)Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool(). The stage is completed with a Boolean that indicates whether or not the lock was acquired.- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)timeout- max timeout to acquire lockunit- time unit of timeout- Returns:
- stage
-
lockAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> lockAsync(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
Attempt to acquire the given lock asynchronously without timeout using theForkJoinPool.commonPool().- Parameters:
lock- a lock implementation (e.g.InterProcessMutex,InterProcessSemaphoreV2, etc.)- Returns:
- stage
-
release
public static void release(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
Release the lock and wrap any exception inRuntimeException- Parameters:
lock- lock to release
-
release
public static void release(org.apache.curator.framework.recipes.locks.InterProcessLock lock, boolean ignoreNoLockExceptions)Release the lock and wrap any exception inRuntimeException- Parameters:
lock- lock to releaseignoreNoLockExceptions- if trueIllegalStateExceptionis ignored
-
-