Class FutureUtil

java.lang.Object
org.apache.pulsar.common.util.FutureUtil

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

    • FutureUtil

      public FutureUtil()
  • Method Details

    • waitForAll

      public static CompletableFuture<Void> waitForAll(Collection<? extends 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
    • waitForAll

      public static <T> CompletableFuture<List<T>> waitForAll(Stream<CompletableFuture<List<T>>> futures)
    • waitForAny

      public static CompletableFuture<Object> waitForAny(Collection<? extends 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 CompletableFuture<Optional<Object>> waitForAny(Collection<? extends CompletableFuture<?>> futures, Predicate<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 CompletableFuture<Void> waitForAllAndSupportCancel(Collection<? extends 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(CompletableFuture<?> future, 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> CompletableFuture<T> failedFuture(Throwable t)
    • unwrapCompletionException

      public static Throwable unwrapCompletionException(Throwable ex)
    • createFutureWithTimeout

      public static <T> CompletableFuture<T> createFutureWithTimeout(Duration timeout, ScheduledExecutorService executor, Supplier<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> CompletableFuture<T> addTimeoutHandling(CompletableFuture<T> future, Duration timeout, ScheduledExecutorService executor, Supplier<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
    • composeAsync

      @Nonnull public static <T> CompletableFuture<T> composeAsync(Supplier<CompletableFuture<T>> futureSupplier, Executor executor)
      Throws:
      RejectedExecutionException - if this task cannot be accepted for execution
      NullPointerException - if one of params is null
    • createTimeoutException

      public static TimeoutException createTimeoutException(String message, Class<?> sourceClass, 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> Optional<Throwable> getException(CompletableFuture<T> future)
    • wrapToCompletionException

      public static CompletionException wrapToCompletionException(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(Runnable runnable, Executor executor, 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