Class AsyncWrappers


  • public class AsyncWrappers
    extends java.lang.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
         });
     

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AsyncWrappers.TimeoutException
      Set 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 containers
      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
      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.
      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 the ForkJoinPool.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 the ForkJoinPool.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 the ForkJoinPool.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 void release​(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
      Release the lock and wrap any exception in RuntimeException
      static void release​(org.apache.curator.framework.recipes.locks.InterProcessLock lock, boolean ignoreNoLockExceptions)
      Release the lock and wrap any exception in RuntimeException
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.NoNodeException is 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.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 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 - client
        path - 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 - client
        path - 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 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 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 lock
        unit - time unit of timeout
        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,
                                                                                     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 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 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 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 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 the ForkJoinPool.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 in RuntimeException
        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 in RuntimeException
        Parameters:
        lock - lock to release
        ignoreNoLockExceptions - if true IllegalStateException is ignored