Package org.pipecraft.infra.io
Class Retrier
- java.lang.Object
-
- org.pipecraft.infra.io.Retrier
-
- All Implemented Interfaces:
JsonMonitorable
public class Retrier extends Object implements JsonMonitorable
Runs aFailableInterruptibleRunnableorFailableInterruptibleSupplierwith 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 Summary
Constructors Constructor Description Retrier(int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,? extends JsonMonitorable>getChildren()net.minidev.json.JSONObjectgetOwnMetrics()<E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun)Runs the given task with the retry configuration as defined in the constructor, and using onlyRuntimeExceptionandErroras early exit errors.static <E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configuration<E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun, Collection<Class<? extends E>> terminalErrorTypes)Runs the given task with the retry configuration as defined in the constructorstatic <E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun, Collection<Class<? extends E>> terminalErrorTypes, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configuration<E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun, Predicate<E> terminalErrorPredicate)Runs the given task with the retry configuration as defined in the constructorstatic <E extends Exception>
voidrun(FailableInterruptibleRunnable<E> toRun, Predicate<E> terminalErrorPredicate, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configuration<T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun)Runs the given task with the retry configuration as defined in the constructor, and using onlyRuntimeExceptionandErroras early exit errors.static <T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configuration<T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun, Collection<Class<? extends E>> terminalErrorTypes)Runs the given task with the retry configuration as defined in the constructorstatic <T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun, Collection<Class<? extends E>> terminalErrorTypes, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configuration<T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun, Predicate<E> terminalErrorPredicate)Runs the given task with the retry configuration as defined in the constructorstatic <T,E extends Exception>
Trun(FailableInterruptibleSupplier<T,E> toRun, Predicate<E> terminalErrorPredicate, int initialWaitTimeMs, double waitTimeFactor, int timesToTry)Runs the given task with the given retry configurationstatic <E extends Exception>
voidrunWithDefaults(FailableInterruptibleRunnable<E> toRun)Runs the given task with the default retry configurationstatic <E extends Exception>
voidrunWithDefaults(FailableInterruptibleRunnable<E> toRun, Collection<Class<? extends E>> terminalErrorTypes)Runs the given task with the default retry configurationstatic <E extends Exception>
voidrunWithDefaults(FailableInterruptibleRunnable<E> toRun, Predicate<E> terminalErrorPredicate)Runs the given task with the default retry configurationstatic <T,E extends Exception>
TrunWithDefaults(FailableInterruptibleSupplier<T,E> toRun)Runs the given task with the default retry configurationstatic <T,E extends Exception>
TrunWithDefaults(FailableInterruptibleSupplier<T,E> toRun, Collection<Class<? extends E>> terminalErrorTypes)Runs the given task with the default retry configurationstatic <T,E extends Exception>
TrunWithDefaults(FailableInterruptibleSupplier<T,E> toRun, Predicate<E> terminalErrorPredicate)Runs the given task with the default retry configuration-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.pipecraft.infra.monitoring.JsonMonitorable
getFullMetrics
-
-
-
-
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
-
run
public <E extends Exception> void run(FailableInterruptibleRunnable<E> toRun) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor, and using onlyRuntimeExceptionandErroras early exit errors.- Parameters:
toRun- The failable runnable to run- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
run
public <T,E extends Exception> T run(FailableInterruptibleSupplier<T,E> toRun) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor, and using onlyRuntimeExceptionandErroras early exit errors.- Parameters:
toRun- The failable supplier to run- Returns:
- The value produced by the supplier
- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
run
public <E extends Exception> void run(FailableInterruptibleRunnable<E> toRun, Predicate<E> terminalErrorPredicate) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor- Parameters:
toRun- The failable runnable to runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
run
public <E extends Exception> void run(FailableInterruptibleRunnable<E> toRun, Collection<Class<? extends E>> terminalErrorTypes) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor- Parameters:
toRun- The failable runnable to runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
run
public <T,E extends Exception> T run(FailableInterruptibleSupplier<T,E> toRun, Collection<Class<? extends E>> terminalErrorTypes) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor- Parameters:
toRun- The failable supplier to runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Returns:
- The value produced by the supplier
- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
run
public <T,E extends Exception> T run(FailableInterruptibleSupplier<T,E> toRun, Predicate<E> terminalErrorPredicate) throws E extends Exception, InterruptedException
Runs the given task with the retry configuration as defined in the constructor- Parameters:
toRun- The failable runnable to runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
runWithDefaults
public static <E extends Exception> void runWithDefaults(FailableInterruptibleRunnable<E> toRun, Predicate<E> terminalErrorPredicate) throws E extends Exception, InterruptedException
Runs the given task with the default retry configuration- Parameters:
toRun- The failable runnable to runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
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 runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare 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 failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
runWithDefaults
public static <E extends Exception> void runWithDefaults(FailableInterruptibleRunnable<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 runnable to runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE 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 runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare 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 failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
runWithDefaults
public static <E extends Exception> void runWithDefaults(FailableInterruptibleRunnable<E> toRun) throws E extends Exception, InterruptedException
Runs the given task with the default retry configuration- Parameters:
toRun- The failable runnable to run- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE 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 failedInterruptedException- In case of an interruption during the retry loopE 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 runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.initialWaitTimeMs- The initial wait time after the first failure, in millisecondswaitTimeFactor- The factor by which wait times increasetimesToTry- The maximum times to attempt the run- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE 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 runterminalErrorPredicate- A predicate indicating what kind of exceptions should not trigger retries, and should exit immediately instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.initialWaitTimeMs- The initial wait time after the first failure, in millisecondswaitTimeFactor- The factor by which wait times increasetimesToTry- 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 failedInterruptedException- In case of an interruption during the retry loopE 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 runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.initialWaitTimeMs- The initial wait time after the first failure, in millisecondswaitTimeFactor- The factor by which wait times increasetimesToTry- The maximum times to attempt the run- Throws:
E- In case of a terminal error or in case that all retries failedInterruptedException- In case of an interruption during the retry loopE 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 runterminalErrorTypes- A set of exceptions that should not trigger retries, and should casue immediate exit instead. Note thatRuntimeException,ErrorandInterruptedExceptionare always handled as terminal errors.initialWaitTimeMs- The initial wait time after the first failure, in millisecondswaitTimeFactor- The factor by which wait times increasetimesToTry- 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 failedInterruptedException- In case of an interruption during the retry loopE 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 runinitialWaitTimeMs- 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 failedInterruptedException- In case of an interruption during the retry loopE 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 runinitialWaitTimeMs- 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 failedInterruptedException- In case of an interruption during the retry loopE extends Exception
-
getOwnMetrics
public net.minidev.json.JSONObject getOwnMetrics()
- Specified by:
getOwnMetricsin interfaceJsonMonitorable- Returns:
- The json string describing the entity, not including the child entities.
-
getChildren
public Map<String,? extends JsonMonitorable> getChildren()
- Specified by:
getChildrenin interfaceJsonMonitorable- 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.
-
-