Class AsyncWrappers
Utility 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
Modifier and TypeClassDescriptionstatic class
Set as the completion stage's exception when trying to acquire a lock times out -
Method Summary
Modifier and TypeMethodDescriptionstatic CompletionStage
<Void> asyncEnsureContainers
(AsyncCuratorFramework client, String path) Asynchronously ensure that the parents of the given path are created as containersstatic CompletionStage
<Void> asyncEnsureParents
(AsyncCuratorFramework client, String path) Asynchronously ensure that the parents of the given path are createdstatic CompletionStage
<Map<String, byte[]>> childrenWithData
(AsyncCuratorFramework client, String path) Return the children of the given path (keyed by the full path) and the data for each node.static CompletionStage
<Map<String, byte[]>> childrenWithData
(AsyncCuratorFramework client, String path, boolean isCompressed) Return the children of the given path (keyed by the full path) and the data for each node.static CompletionStage
<Void> lockAsync
(InterProcessLock lock) Attempt to acquire the given lock asynchronously without timeout using theForkJoinPool.commonPool()
.static CompletionStage
<Void> lockAsync
(InterProcessLock lock, long timeout, TimeUnit unit) Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool()
.static CompletionStage
<Void> lockAsync
(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor) Attempt to acquire the given lock asynchronously using the given timeout and executor.static CompletionStage
<Void> lockAsync
(InterProcessLock lock, Executor executor) Attempt to acquire the given lock asynchronously using the given executor and without a timeout.static CompletionStage
<Boolean> lockAsyncIf
(InterProcessLock lock, long timeout, TimeUnit unit) Attempt to acquire the given lock asynchronously using the given timeout using theForkJoinPool.commonPool()
.static CompletionStage
<Boolean> lockAsyncIf
(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor) Attempt to acquire the given lock asynchronously using the given timeout and executor.static void
release
(InterProcessLock lock) Release the lock and wrap any exception inRuntimeException
static void
release
(InterProcessLock lock, boolean ignoreNoLockExceptions) Release the lock and wrap any exception inRuntimeException
-
Method Details
-
childrenWithData
public static CompletionStage<Map<String,byte[]>> childrenWithData(AsyncCuratorFramework client, 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.NoNodeException
is NOT set. Instead the stage is completed with an empty map.- Returns:
- CompletionStage
-
childrenWithData
public static CompletionStage<Map<String,byte[]>> childrenWithData(AsyncCuratorFramework client, 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.NoNodeException
is NOT set. Instead the stage is completed with an empty map.- Parameters:
isCompressed
- pass true if data is compressed- Returns:
- CompletionStage
-
asyncEnsureParents
Asynchronously ensure that the parents of the given path are created- Parameters:
client
- clientpath
- path to ensure- Returns:
- stage
-
asyncEnsureContainers
public static CompletionStage<Void> asyncEnsureContainers(AsyncCuratorFramework client, 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 CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit, 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 CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit, 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
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
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 CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, 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
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
Release the lock and wrap any exception inRuntimeException
- Parameters:
lock
- lock to release
-
release
Release the lock and wrap any exception inRuntimeException
- Parameters:
lock
- lock to releaseignoreNoLockExceptions
- if trueIllegalStateException
is ignored
-