Class FutureUtil


  • public class FutureUtil
    extends java.lang.Object
    This class is aimed at simplifying work with CompletableFuture.
    • Constructor Summary

      Constructors 
      Constructor Description
      FutureUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> java.util.concurrent.CompletableFuture<T> addTimeoutHandling​(java.util.concurrent.CompletableFuture<T> future, java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService executor, java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
      Adds timeout handling to an existing CompletableFuture.
      static <T> java.util.concurrent.CompletableFuture<T> createFutureWithTimeout​(java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService executor, java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
      Creates a new CompletableFuture instance with timeout handling.
      static java.util.concurrent.TimeoutException createTimeoutException​(java.lang.String message, java.lang.Class<?> sourceClass, java.lang.String sourceMethod)
      Creates a low-overhead timeout exception which is performance optimized to minimize allocations and cpu consumption.
      static <T> java.util.concurrent.CompletableFuture<T> failedFuture​(java.lang.Throwable t)  
      static <T> java.util.Optional<java.lang.Throwable> getException​(java.util.concurrent.CompletableFuture<T> future)  
      static void safeRunAsync​(java.lang.Runnable runnable, java.util.concurrent.Executor executor, java.util.concurrent.CompletableFuture completableFuture)
      Executes an operation using the supplied Executor and notify failures on the supplied CompletableFuture.
      static java.lang.Throwable unwrapCompletionException​(java.lang.Throwable ex)  
      static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAll​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
      Return a future that represents the completion of the futures in the provided Collection.
      static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAll​(java.util.List<? extends java.util.concurrent.CompletableFuture<?>> futures)
      Deprecated.
      static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAllAndSupportCancel​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
      Return a future that represents the completion of the futures in the provided Collection.
      static java.util.concurrent.CompletableFuture<java.lang.Object> waitForAny​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
      Return a future that represents the completion of any future in the provided Collection.
      static java.util.concurrent.CompletableFuture<java.util.Optional<java.lang.Object>> waitForAny​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures, java.util.function.Predicate<java.lang.Object> tester)
      Return a future that represents the completion of any future that match the predicate in the provided Collection.
      static java.util.concurrent.CompletableFuture<java.lang.Object> waitForAny​(java.util.List<? extends java.util.concurrent.CompletableFuture<?>> futures)
      Deprecated.
      static void whenCancelledOrTimedOut​(java.util.concurrent.CompletableFuture<?> future, java.lang.Runnable cancelAction)
      If the future is cancelled or times out, the cancel action will be invoked.
      static java.util.concurrent.CompletionException wrapToCompletionException​(java.lang.Throwable throwable)
      Wrap throwable exception to CompletionException if that exception is not an instance of CompletionException.
      • Methods inherited from class java.lang.Object

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

      • FutureUtil

        public FutureUtil()
    • Method Detail

      • waitForAll

        @Deprecated
        public static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAll​(java.util.List<? extends java.util.concurrent.CompletableFuture<?>> futures)
        Deprecated.
        Return a future that represents the completion of the futures in the provided List. This method with the List parameter is needed to keep compatibility with external applications that are compiled with Pulsar < 2.10.0.
        Parameters:
        futures - futures to wait for
        Returns:
        a new CompletableFuture that is completed when all of the given CompletableFutures complete
      • waitForAll

        public static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAll​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
        Return a future that represents the completion of the futures in the provided Collection.
        Parameters:
        futures - futures to wait for
        Returns:
        a new CompletableFuture that is completed when all of the given CompletableFutures complete
      • waitForAny

        @Deprecated
        public static java.util.concurrent.CompletableFuture<java.lang.Object> waitForAny​(java.util.List<? extends java.util.concurrent.CompletableFuture<?>> futures)
        Deprecated.
        Return a future that represents the completion of any future in the provided List. This method with the List parameter is needed to keep compatibility with external applications that are compiled with Pulsar < 2.10.0.
        Parameters:
        futures - futures to wait any
        Returns:
        a new CompletableFuture that is completed when any of the given CompletableFutures complete
      • waitForAny

        public static java.util.concurrent.CompletableFuture<java.lang.Object> waitForAny​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
        Return a future that represents the completion of any future in the provided Collection.
        Parameters:
        futures - futures to wait any
        Returns:
        a new CompletableFuture that is completed when any of the given CompletableFutures complete
      • waitForAny

        public static java.util.concurrent.CompletableFuture<java.util.Optional<java.lang.Object>> waitForAny​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures,
                                                                                                              java.util.function.Predicate<java.lang.Object> tester)
        Return a future that represents the completion of any future that match the predicate in the provided Collection.
        Parameters:
        futures - futures to wait any
        tester - if any future match the predicate
        Returns:
        a new CompletableFuture that is completed when any of the given CompletableFutures match the tester
      • waitForAllAndSupportCancel

        public static java.util.concurrent.CompletableFuture<java.lang.Void> waitForAllAndSupportCancel​(java.util.Collection<? extends java.util.concurrent.CompletableFuture<?>> futures)
        Return a future that represents the completion of the futures in the provided Collection. The future will support CompletableFuture.cancel(boolean). It will cancel all unfinished futures when the future gets cancelled.
        Parameters:
        futures - futures to wait for
        Returns:
        a new CompletableFuture that is completed when all of the given CompletableFutures complete
      • whenCancelledOrTimedOut

        public static void whenCancelledOrTimedOut​(java.util.concurrent.CompletableFuture<?> future,
                                                   java.lang.Runnable cancelAction)
        If the future is cancelled or times out, the cancel action will be invoked. The action is executed once if the future completes with CancellationException or TimeoutException
        Parameters:
        future - future to attach the action to
        cancelAction - action to invoke if the future is cancelled or times out
      • failedFuture

        public static <T> java.util.concurrent.CompletableFuture<T> failedFuture​(java.lang.Throwable t)
      • unwrapCompletionException

        public static java.lang.Throwable unwrapCompletionException​(java.lang.Throwable ex)
      • createFutureWithTimeout

        public static <T> java.util.concurrent.CompletableFuture<T> createFutureWithTimeout​(java.time.Duration timeout,
                                                                                            java.util.concurrent.ScheduledExecutorService executor,
                                                                                            java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
        Creates a new CompletableFuture instance with timeout handling.
        Type Parameters:
        T - type parameter for the future
        Parameters:
        timeout - the duration of the timeout
        executor - the executor to use for scheduling the timeout
        exceptionSupplier - the supplier for creating the exception
        Returns:
        the new CompletableFuture instance
      • addTimeoutHandling

        public static <T> java.util.concurrent.CompletableFuture<T> addTimeoutHandling​(java.util.concurrent.CompletableFuture<T> future,
                                                                                       java.time.Duration timeout,
                                                                                       java.util.concurrent.ScheduledExecutorService executor,
                                                                                       java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
        Adds timeout handling to an existing CompletableFuture.
        Type Parameters:
        T - type parameter for the future
        Parameters:
        future - the target future
        timeout - the duration of the timeout
        executor - the executor to use for scheduling the timeout
        exceptionSupplier - the supplier for creating the exception
        Returns:
        returns the original target future
      • createTimeoutException

        public static java.util.concurrent.TimeoutException createTimeoutException​(java.lang.String message,
                                                                                   java.lang.Class<?> sourceClass,
                                                                                   java.lang.String sourceMethod)
        Creates a low-overhead timeout exception which is performance optimized to minimize allocations and cpu consumption. It sets the stacktrace of the exception to the given source class and source method name. The instances of this class can be cached or stored as constants and reused multiple times.
        Parameters:
        message - exception message
        sourceClass - source class for manually filled in stacktrace
        sourceMethod - source method name for manually filled in stacktrace
        Returns:
        new TimeoutException instance
      • getException

        public static <T> java.util.Optional<java.lang.Throwable> getException​(java.util.concurrent.CompletableFuture<T> future)
      • wrapToCompletionException

        public static java.util.concurrent.CompletionException wrapToCompletionException​(java.lang.Throwable throwable)
        Wrap throwable exception to CompletionException if that exception is not an instance of CompletionException.
        Parameters:
        throwable - Exception
        Returns:
        CompletionException
      • safeRunAsync

        public static void safeRunAsync​(java.lang.Runnable runnable,
                                        java.util.concurrent.Executor executor,
                                        java.util.concurrent.CompletableFuture completableFuture)
        Executes an operation using the supplied Executor and notify failures on the supplied CompletableFuture.
        Parameters:
        runnable - the runnable to execute
        executor - the executor to use for executing the runnable
        completableFuture - the future to complete in case of exceptions