public class AsyncWrappers extends Object
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
});
Modifier and Type | Class and Description |
---|---|
static class |
AsyncWrappers.TimeoutException
Set as the completion stage's exception when trying to acquire a lock
times out
|
Modifier and Type | Method and Description |
---|---|
static CompletionStage<Void> |
asyncEnsureContainers(AsyncCuratorFramework client,
String path)
Asynchronously ensure that the parents of the given path are created as containers
|
static CompletionStage<Void> |
asyncEnsureParents(AsyncCuratorFramework client,
String path)
Asynchronously ensure that the parents of the given path are created
|
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.
|
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 the
ForkJoinPool.commonPool() . |
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<Void> |
lockAsync(InterProcessLock lock,
long timeout,
TimeUnit unit)
Attempt to acquire the given lock asynchronously using the given timeout using the
ForkJoinPool.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<Boolean> |
lockAsyncIf(InterProcessLock lock,
long timeout,
TimeUnit unit)
Attempt to acquire the given lock asynchronously using the given timeout using the
ForkJoinPool.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 in
RuntimeException |
static void |
release(InterProcessLock lock,
boolean ignoreNoLockExceptions)
Release the lock and wrap any exception in
RuntimeException |
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.
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.
isCompressed
- pass true if data is compressedpublic static CompletionStage<Void> asyncEnsureParents(AsyncCuratorFramework client, String path)
client
- clientpath
- path to ensurepublic static CompletionStage<Void> asyncEnsureContainers(AsyncCuratorFramework client, String path)
client
- clientpath
- path to ensurepublic static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor)
AsyncWrappers.TimeoutException
lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeoutexecutor
- executor to use to asynchronously acquirepublic static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor)
lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeoutexecutor
- executor to use to asynchronously acquirepublic static CompletionStage<Void> lockAsync(InterProcessLock lock, Executor executor)
lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)executor
- executor to use to asynchronously acquirepublic static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit)
ForkJoinPool.commonPool()
.
If the lock is not acquired within the timeout stage is completedExceptionally with AsyncWrappers.TimeoutException
lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeoutpublic static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit)
ForkJoinPool.commonPool()
.
The stage is completed with a Boolean that indicates whether or not the lock was acquired.lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeoutpublic static CompletionStage<Void> lockAsync(InterProcessLock lock)
ForkJoinPool.commonPool()
.lock
- a lock implementation (e.g. InterProcessMutex
,
InterProcessSemaphoreV2
, etc.)public static void release(InterProcessLock lock)
RuntimeException
lock
- lock to releasepublic static void release(InterProcessLock lock, boolean ignoreNoLockExceptions)
RuntimeException
lock
- lock to releaseignoreNoLockExceptions
- if true IllegalStateException
is ignoredCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.