Class FutureUtils


  • public final class FutureUtils
    extends java.lang.Object
    Future related utils.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> collect​(java.util.List<java.util.concurrent.CompletableFuture<T>> futureList)  
      static <T> void complete​(java.util.concurrent.CompletableFuture<T> result, T value)  
      static <T> void completeExceptionally​(java.util.concurrent.CompletableFuture<T> result, java.lang.Throwable cause)  
      static <T> java.util.concurrent.CompletableFuture<T> createFuture()  
      static <T> java.util.concurrent.CompletableFuture<T> ensure​(java.util.concurrent.CompletableFuture<T> future, java.lang.Runnable ensureBlock)  
      static <T> java.util.concurrent.CompletableFuture<T> exception​(java.lang.Throwable cause)  
      static <T> java.util.concurrent.CompletableFuture<java.lang.Void> ignore​(java.util.concurrent.CompletableFuture<T> future)
      Ignore exception from the future.
      static <T> java.util.concurrent.CompletableFuture<java.lang.Void> ignore​(java.util.concurrent.CompletableFuture<T> future, java.lang.String errorMsg)
      Ignore exception from the future and log errorMsg on exceptions.
      static <T,​R>
      java.util.concurrent.CompletableFuture<java.util.List<R>>
      processList​(java.util.List<T> collection, java.util.function.Function<T,​java.util.concurrent.CompletableFuture<R>> processFunc, java.util.concurrent.ExecutorService callbackExecutor)
      Process the list of items one by one using the process function processFunc.
      static <T> void proxyTo​(java.util.concurrent.CompletableFuture<T> src, java.util.concurrent.CompletableFuture<T> target)  
      static <T> java.util.concurrent.CompletableFuture<T> rescue​(java.util.concurrent.CompletableFuture<T> future, java.util.function.Function<java.lang.Throwable,​java.util.concurrent.CompletableFuture<T>> rescueFuc)  
      static <T> T result​(java.util.concurrent.CompletableFuture<T> future)  
      static <T> T result​(java.util.concurrent.CompletableFuture<T> future, long timeout, java.util.concurrent.TimeUnit timeUnit)  
      static <T,​ExceptionT extends java.lang.Throwable>
      T
      result​(java.util.concurrent.CompletableFuture<T> future, java.util.function.Function<java.lang.Throwable,​ExceptionT> exceptionHandler)  
      static <T,​ExceptionT extends java.lang.Throwable>
      T
      result​(java.util.concurrent.CompletableFuture<T> future, java.util.function.Function<java.lang.Throwable,​ExceptionT> exceptionHandler, long timeout, java.util.concurrent.TimeUnit timeUnit)  
      static <T> java.util.concurrent.CompletableFuture<T> stats​(java.util.concurrent.CompletableFuture<T> result, OpStatsLogger opStatsLogger, com.google.common.base.Stopwatch stopwatch)
      Add a event listener over result for collecting the operation stats.
      static <T> java.util.concurrent.CompletableFuture<T> value​(T value)  
      static java.util.concurrent.CompletableFuture<java.lang.Void> Void()  
      static <T> java.util.concurrent.CompletableFuture<T> whenCompleteAsync​(java.util.concurrent.CompletableFuture<T> future, java.util.function.BiConsumer<? super T,​? super java.lang.Throwable> action, OrderedScheduler scheduler, java.lang.Object scheduleKey)
      Completing the future in the thread in the scheduler identified by the scheduleKey.
      static <T> java.util.concurrent.CompletableFuture<T> within​(java.util.concurrent.CompletableFuture<T> promise, long timeout, java.util.concurrent.TimeUnit unit, java.lang.Throwable cause, OrderedScheduler scheduler, java.lang.Object key)
      Raise an exception to the promise within a given timeout period.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • Void

        public static java.util.concurrent.CompletableFuture<java.lang.Void> Void()
      • result

        public static <T> T result​(java.util.concurrent.CompletableFuture<T> future)
                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • result

        public static <T> T result​(java.util.concurrent.CompletableFuture<T> future,
                                   long timeout,
                                   java.util.concurrent.TimeUnit timeUnit)
                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • result

        public static <T,​ExceptionT extends java.lang.Throwable> T result​(java.util.concurrent.CompletableFuture<T> future,
                                                                                java.util.function.Function<java.lang.Throwable,​ExceptionT> exceptionHandler)
                                                                         throws ExceptionT extends java.lang.Throwable
        Throws:
        ExceptionT extends java.lang.Throwable
      • result

        public static <T,​ExceptionT extends java.lang.Throwable> T result​(java.util.concurrent.CompletableFuture<T> future,
                                                                                java.util.function.Function<java.lang.Throwable,​ExceptionT> exceptionHandler,
                                                                                long timeout,
                                                                                java.util.concurrent.TimeUnit timeUnit)
                                                                         throws ExceptionT extends java.lang.Throwable,
                                                                                java.util.concurrent.TimeoutException
        Throws:
        ExceptionT extends java.lang.Throwable
        java.util.concurrent.TimeoutException
      • createFuture

        public static <T> java.util.concurrent.CompletableFuture<T> createFuture()
      • value

        public static <T> java.util.concurrent.CompletableFuture<T> value​(T value)
      • exception

        public static <T> java.util.concurrent.CompletableFuture<T> exception​(java.lang.Throwable cause)
      • complete

        public static <T> void complete​(java.util.concurrent.CompletableFuture<T> result,
                                        T value)
      • completeExceptionally

        public static <T> void completeExceptionally​(java.util.concurrent.CompletableFuture<T> result,
                                                     java.lang.Throwable cause)
      • whenCompleteAsync

        public static <T> java.util.concurrent.CompletableFuture<T> whenCompleteAsync​(java.util.concurrent.CompletableFuture<T> future,
                                                                                      java.util.function.BiConsumer<? super T,​? super java.lang.Throwable> action,
                                                                                      OrderedScheduler scheduler,
                                                                                      java.lang.Object scheduleKey)
        Completing the future in the thread in the scheduler identified by the scheduleKey.
        Type Parameters:
        T -
        Parameters:
        future - future to complete
        action - action to execute when complete
        scheduler - scheduler to execute the action.
        scheduleKey - key to choose the thread to execute the action
        Returns:
      • collect

        public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> collect​(java.util.List<java.util.concurrent.CompletableFuture<T>> futureList)
      • proxyTo

        public static <T> void proxyTo​(java.util.concurrent.CompletableFuture<T> src,
                                       java.util.concurrent.CompletableFuture<T> target)
      • processList

        public static <T,​R> java.util.concurrent.CompletableFuture<java.util.List<R>> processList​(java.util.List<T> collection,
                                                                                                        java.util.function.Function<T,​java.util.concurrent.CompletableFuture<R>> processFunc,
                                                                                                        @Nullable
                                                                                                        java.util.concurrent.ExecutorService callbackExecutor)
        Process the list of items one by one using the process function processFunc. The process will be stopped immediately if it fails on processing any one.
        Parameters:
        collection - list of items
        processFunc - process function
        callbackExecutor - executor to process the item
        Returns:
        future presents the list of processed results
      • within

        public static <T> java.util.concurrent.CompletableFuture<T> within​(java.util.concurrent.CompletableFuture<T> promise,
                                                                           long timeout,
                                                                           java.util.concurrent.TimeUnit unit,
                                                                           java.lang.Throwable cause,
                                                                           OrderedScheduler scheduler,
                                                                           java.lang.Object key)
        Raise an exception to the promise within a given timeout period. If the promise has been satisfied before raising, it won't change the state of the promise.
        Parameters:
        promise - promise to raise exception
        timeout - timeout period
        unit - timeout period unit
        cause - cause to raise
        scheduler - scheduler to execute raising exception
        key - the submit key used by the scheduler
        Returns:
        the promise applied with the raise logic
      • ignore

        public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> ignore​(java.util.concurrent.CompletableFuture<T> future)
        Ignore exception from the future.
        Parameters:
        future - the original future
        Returns:
        a transformed future ignores exceptions
      • ignore

        public static <T> java.util.concurrent.CompletableFuture<java.lang.Void> ignore​(java.util.concurrent.CompletableFuture<T> future,
                                                                                        java.lang.String errorMsg)
        Ignore exception from the future and log errorMsg on exceptions.
        Parameters:
        future - the original future
        errorMsg - the error message to log on exceptions
        Returns:
        a transformed future ignores exceptions
      • ensure

        public static <T> java.util.concurrent.CompletableFuture<T> ensure​(java.util.concurrent.CompletableFuture<T> future,
                                                                           java.lang.Runnable ensureBlock)
      • rescue

        public static <T> java.util.concurrent.CompletableFuture<T> rescue​(java.util.concurrent.CompletableFuture<T> future,
                                                                           java.util.function.Function<java.lang.Throwable,​java.util.concurrent.CompletableFuture<T>> rescueFuc)
      • stats

        public static <T> java.util.concurrent.CompletableFuture<T> stats​(java.util.concurrent.CompletableFuture<T> result,
                                                                          OpStatsLogger opStatsLogger,
                                                                          com.google.common.base.Stopwatch stopwatch)
        Add a event listener over result for collecting the operation stats.
        Type Parameters:
        T -
        Parameters:
        result - result to listen on
        opStatsLogger - stats logger to record operations stats
        stopwatch - stop watch to time operation
        Returns:
        result after registered the event listener