Class AsyncUtils


  • public class AsyncUtils
    extends Object
    • Method Detail

      • withTimeout

        public static <T> CompletableFuture<T> withTimeout​(CompletableFuture<T> future,
                                                           Duration timeout)
        Returns the provided CompletableFuture that will complete exceptionally with a TimeoutException if the provided Duration timeout period is exceeded.
        Type Parameters:
        T - the result type returned by the future.
        Parameters:
        future - the future to add a timeout to.
        timeout - the timeout duration.
        Returns:
        the provided future with a timeout.
      • retryWithExponentialBackoff

        public static <T> CompletableFuture<T> retryWithExponentialBackoff​(Supplier<CompletableFuture<T>> action,
                                                                           Consumer<T> onCancel,
                                                                           Duration timeout,
                                                                           ExponentialBackoffIntervalCalculator retryIntervalCalculator,
                                                                           AsyncUtils.ShouldRetry<T> shouldRetry)
        Returns a new CompletableFuture that will complete once the action provided by the action supplier completes. The action will be retried with an exponential backoff using the ExponentialBackoffIntervalCalculator as long as the AsyncUtils.ShouldRetry predicate returns a non-negative value. Each action retrieval retry will time out after the provided timeout Duration.
        Type Parameters:
        T - the result type returned by the future.
        Parameters:
        action - the action supplier.
        onCancel - consumes the intermediate result in case the returned future is cancelled or each time the action is retried.
        timeout - the timeout duration.
        retryIntervalCalculator - the retry interval calculator.
        shouldRetry - the predicate to compute if the action is to be retried.
        Returns:
        a new CompletableFuture that will complete once the action provided by the action supplier completes.