Class AsyncWrappers

java.lang.Object
org.apache.curator.x.async.AsyncWrappers

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
     });
 

  • 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

      public static CompletionStage<Void> asyncEnsureParents(AsyncCuratorFramework client, String path)
      Asynchronously ensure that the parents of the given path are created
      Parameters:
      client - client
      path - 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 - client
      path - 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 with AsyncWrappers.TimeoutException
      Parameters:
      lock - a lock implementation (e.g. InterProcessMutex, InterProcessSemaphoreV2, etc.)
      timeout - max timeout to acquire lock
      unit - time unit of timeout
      executor - 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 lock
      unit - time unit of timeout
      executor - executor to use to asynchronously acquire
      Returns:
      stage
    • lockAsync

      public static CompletionStage<Void> lockAsync(InterProcessLock lock, 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 CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit)
      Attempt to acquire the given lock asynchronously using the given timeout using the ForkJoinPool.commonPool(). If the lock is not acquired within the timeout stage is completedExceptionally with AsyncWrappers.TimeoutException
      Parameters:
      lock - a lock implementation (e.g. InterProcessMutex, InterProcessSemaphoreV2, etc.)
      timeout - max timeout to acquire lock
      unit - 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 the ForkJoinPool.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 lock
      unit - time unit of timeout
      Returns:
      stage
    • lockAsync

      public static CompletionStage<Void> lockAsync(InterProcessLock lock)
      Attempt to acquire the given lock asynchronously without timeout using the ForkJoinPool.commonPool().
      Parameters:
      lock - a lock implementation (e.g. InterProcessMutex, InterProcessSemaphoreV2, etc.)
      Returns:
      stage
    • release

      public static void release(InterProcessLock lock)
      Release the lock and wrap any exception in RuntimeException
      Parameters:
      lock - lock to release
    • release

      public static void release(InterProcessLock lock, boolean ignoreNoLockExceptions)
      Release the lock and wrap any exception in RuntimeException
      Parameters:
      lock - lock to release
      ignoreNoLockExceptions - if true IllegalStateException is ignored