Class Retrier

  • All Implemented Interfaces:
    JsonMonitorable

    public class Retrier
    extends Object
    implements JsonMonitorable
    Runs a FailableInterruptibleRunnable or FailableInterruptibleSupplier with configurable retries. Thread safe as long as the runnables passed to it are thread safe. This class is Monitorable, and provides the following metrics: - invocations - Total number of API invocations - failedInvocations - Total number of invocations ultimately failing, after retries - tries - Total number of attempts
    Author:
    Eyal Schneider
    • Constructor Detail

      • Retrier

        public Retrier​(int initialWaitTimeMs,
                       double waitTimeFactor,
                       int timesToTry)
        Constructor
        Parameters:
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds. Must be non-negative.
        waitTimeFactor - The factor by which wait times increase. Should be greater than 1.0.
        timesToTry - The maximum times to attempt the run. Should be greater than 1.
    • Method Detail

      • runWithDefaults

        public static <T,​E extends Exception> T runWithDefaults​(FailableInterruptibleSupplier<T,​E> toRun,
                                                                      Predicate<E> terminalErrorPredicate)
                                                               throws E extends Exception,
                                                                      InterruptedException
        Runs the given task with the default retry configuration
        Parameters:
        toRun - The failable supplier to run
        terminalErrorPredicate - A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • runWithDefaults

        public static <T,​E extends Exception> T runWithDefaults​(FailableInterruptibleSupplier<T,​E> toRun,
                                                                      Collection<Class<? extends E>> terminalErrorTypes)
                                                               throws E extends Exception,
                                                                      InterruptedException
        Runs the given task with the default retry configuration
        Parameters:
        toRun - The failable supplier to run
        terminalErrorTypes - A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • runWithDefaults

        public static <T,​E extends Exception> T runWithDefaults​(FailableInterruptibleSupplier<T,​E> toRun)
                                                               throws E extends Exception,
                                                                      InterruptedException
        Runs the given task with the default retry configuration
        Parameters:
        toRun - The failable runnable to run
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <E extends Exception> void run​(FailableInterruptibleRunnable<E> toRun,
                                                     Predicate<E> terminalErrorPredicate,
                                                     int initialWaitTimeMs,
                                                     double waitTimeFactor,
                                                     int timesToTry)
                                              throws E extends Exception,
                                                     InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable runnable to run
        terminalErrorPredicate - A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds
        waitTimeFactor - The factor by which wait times increase
        timesToTry - The maximum times to attempt the run
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <T,​E extends Exception> T run​(FailableInterruptibleSupplier<T,​E> toRun,
                                                          Predicate<E> terminalErrorPredicate,
                                                          int initialWaitTimeMs,
                                                          double waitTimeFactor,
                                                          int timesToTry)
                                                   throws E extends Exception,
                                                          InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable supplier to run
        terminalErrorPredicate - A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds
        waitTimeFactor - The factor by which wait times increase
        timesToTry - The maximum times to attempt the run
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <E extends Exception> void run​(FailableInterruptibleRunnable<E> toRun,
                                                     Collection<Class<? extends E>> terminalErrorTypes,
                                                     int initialWaitTimeMs,
                                                     double waitTimeFactor,
                                                     int timesToTry)
                                              throws E extends Exception,
                                                     InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable runnable to run
        terminalErrorTypes - A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds
        waitTimeFactor - The factor by which wait times increase
        timesToTry - The maximum times to attempt the run
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <T,​E extends Exception> T run​(FailableInterruptibleSupplier<T,​E> toRun,
                                                          Collection<Class<? extends E>> terminalErrorTypes,
                                                          int initialWaitTimeMs,
                                                          double waitTimeFactor,
                                                          int timesToTry)
                                                   throws E extends Exception,
                                                          InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable supplier to run
        terminalErrorTypes - A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note that RuntimeException, Error and InterruptedException are always handled as terminal errors.
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds
        waitTimeFactor - The factor by which wait times increase
        timesToTry - The maximum times to attempt the run
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <E extends Exception> void run​(FailableInterruptibleRunnable<E> toRun,
                                                     int initialWaitTimeMs,
                                                     double waitTimeFactor,
                                                     int timesToTry)
                                              throws E extends Exception,
                                                     InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable runnable to run
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds. Must be non-negative.
        waitTimeFactor - The factor by which wait times increase. Should be greater than 1.0.
        timesToTry - The maximum times to attempt the run. Should be greater than 1.
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • run

        public static <T,​E extends Exception> T run​(FailableInterruptibleSupplier<T,​E> toRun,
                                                          int initialWaitTimeMs,
                                                          double waitTimeFactor,
                                                          int timesToTry)
                                                   throws E extends Exception,
                                                          InterruptedException
        Runs the given task with the given retry configuration
        Parameters:
        toRun - The failable supplirt to run
        initialWaitTimeMs - The initial wait time after the first failure, in milliseconds. Must be non-negative.
        waitTimeFactor - The factor by which wait times increase. Should be greater than 1.0.
        timesToTry - The maximum times to attempt the run. Should be greater than 1.
        Returns:
        The value produced by the given supplier
        Throws:
        E - In case of a terminal error or in case that all retries failed
        InterruptedException - In case of an interruption during the retry loop
        E extends Exception
      • getOwnMetrics

        public net.minidev.json.JSONObject getOwnMetrics()
        Specified by:
        getOwnMetrics in interface JsonMonitorable
        Returns:
        The json string describing the entity, not including the child entities.
      • getChildren

        public Map<String,​? extends JsonMonitorable> getChildren()
        Specified by:
        getChildren in interface JsonMonitorable
        Returns:
        The monitorable children of this entity, as [id, JsonExportable] pairs. Using this method, the framework can manage a complete tree to be monitored, where every entity has a unique path.