Class Retries


  • public final class Retries
    extends java.lang.Object
    A util class for supporting retries with customized backoff.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.function.Predicate<java.lang.Throwable> NonFatalPredicate  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <ReturnT> java.util.concurrent.CompletableFuture<ReturnT> run​(java.util.stream.Stream<java.lang.Long> backoffs, java.util.function.Predicate<java.lang.Throwable> retryPredicate, java.util.function.Supplier<java.util.concurrent.CompletableFuture<ReturnT>> task, OrderedScheduler scheduler)
      Retry a given task on failures.
      static <ReturnT> java.util.concurrent.CompletableFuture<ReturnT> run​(java.util.stream.Stream<java.lang.Long> backoffs, java.util.function.Predicate<java.lang.Throwable> retryPredicate, java.util.function.Supplier<java.util.concurrent.CompletableFuture<ReturnT>> task, OrderedScheduler scheduler, java.lang.Object key)
      Retry a given task on failures.
      • Methods inherited from class java.lang.Object

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

      • NonFatalPredicate

        public static final java.util.function.Predicate<java.lang.Throwable> NonFatalPredicate
    • Method Detail

      • run

        public static <ReturnT> java.util.concurrent.CompletableFuture<ReturnT> run​(java.util.stream.Stream<java.lang.Long> backoffs,
                                                                                    java.util.function.Predicate<java.lang.Throwable> retryPredicate,
                                                                                    java.util.function.Supplier<java.util.concurrent.CompletableFuture<ReturnT>> task,
                                                                                    OrderedScheduler scheduler)
        Retry a given task on failures.

        It is a shortcut of run(Stream, Predicate, Supplier, OrderedScheduler, Object) that runs retries on any threads in the provided scheduler.

        Type Parameters:
        ReturnT - the return type
        Parameters:
        backoffs - a stream of backoff delays, in milliseconds.
        retryPredicate - a predicate to test if failures are retryable.
        task - a task to execute.
        scheduler - scheduler to schedule the task and complete the futures.
        Returns:
        future represents the result of the task with retries.
      • run

        public static <ReturnT> java.util.concurrent.CompletableFuture<ReturnT> run​(java.util.stream.Stream<java.lang.Long> backoffs,
                                                                                    java.util.function.Predicate<java.lang.Throwable> retryPredicate,
                                                                                    java.util.function.Supplier<java.util.concurrent.CompletableFuture<ReturnT>> task,
                                                                                    OrderedScheduler scheduler,
                                                                                    java.lang.Object key)
        Retry a given task on failures.

        It will only retry the tasks when the predicate retryPredicate tests it as a retryable failure and it doesn't exhaust the retry budget. The retry delays are defined in a stream of delay values (in milliseconds).

        If a schedule key is provided, the task will be submitted to the scheduler using the provided schedule key and also the returned future will be completed in the same thread. Otherwise, the task and the returned future will be executed and scheduled on any threads in the scheduler.

        Type Parameters:
        ReturnT - the return tye.
        Parameters:
        backoffs - a stream of backoff delays, in milliseconds.
        retryPredicate - a predicate to test if failures are retryable.
        task - a task to execute.
        scheduler - scheduler to schedule the task and complete the futures.
        key - the submit key for the scheduler.
        Returns:
        future represents the result of the task with retries.