Package org.keycloak.common.util
Class Retry
java.lang.Object
org.keycloak.common.util.Retry
- Author:
- Stian Thorgersen
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Runnable, which provides some additional info (iteration for now)static interface
Needed here because: - java.util.function.Supplier defined from Java 8 - Adds some additional info (current iteration)static interface
Needed here because: - java.util.function.BiConsumer defined from Java 8 - Adds some additional info (current iteration and called throwable -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
call
(Retry.Supplier<T> supplier, int attemptsCount, long intervalMillis) Runs the givenrunnable
at mostattemptsCount
times until it passes, leavingintervalMillis
milliseconds between the invocations.static int
computeBackoffInterval
(int base, int iteration) static int
Runs the givenrunnable
at mostattemptsCount
times until it passes, leavingintervalMillis
milliseconds between the invocations.static int
executeWithBackoff
(Retry.AdvancedRunnable runnable, int attemptsCount, int intervalBaseMillis) Runs the givenrunnable
at mostattemptsCount
times until it passes, leaving some increasing random delay milliseconds between the invocations.static int
executeWithBackoff
(Retry.AdvancedRunnable runnable, Duration timeout, int intervalBaseMillis) static int
executeWithBackoff
(Retry.AdvancedRunnable runnable, Retry.ThrowableCallback throwableCallback, int attemptsCount, int intervalBaseMillis) static int
executeWithBackoff
(Retry.AdvancedRunnable runnable, Retry.ThrowableCallback throwableCallback, Duration timeout, int intervalBaseMillis)
-
Constructor Details
-
Retry
public Retry()
-
-
Method Details
-
execute
Runs the givenrunnable
at mostattemptsCount
times until it passes, leavingintervalMillis
milliseconds between the invocations. The runnable is reexecuted if it throws aRuntimeException
orAssertionError
.- Parameters:
runnable
-attemptsCount
- Total number of attempts to execute therunnable
intervalMillis
-- Returns:
- Index of the first successful invocation, starting from 0.
-
executeWithBackoff
public static int executeWithBackoff(Retry.AdvancedRunnable runnable, int attemptsCount, int intervalBaseMillis) Runs the givenrunnable
at mostattemptsCount
times until it passes, leaving some increasing random delay milliseconds between the invocations. It uses Exponential backoff + jitter algorithm to compute the delay. More details https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ The base for delay is specified byintervalBaseMillis
number. The runnable is reexecuted if it throws aRuntimeException
orAssertionError
.- Parameters:
runnable
-attemptsCount
- Total number of attempts to execute therunnable
intervalBaseMillis
- base for the exponential backoff + jitter- Returns:
- Index of the first successful invocation, starting from 0.
-
executeWithBackoff
public static int executeWithBackoff(Retry.AdvancedRunnable runnable, Retry.ThrowableCallback throwableCallback, int attemptsCount, int intervalBaseMillis) -
executeWithBackoff
public static int executeWithBackoff(Retry.AdvancedRunnable runnable, Duration timeout, int intervalBaseMillis) -
executeWithBackoff
public static int executeWithBackoff(Retry.AdvancedRunnable runnable, Retry.ThrowableCallback throwableCallback, Duration timeout, int intervalBaseMillis) -
computeBackoffInterval
public static int computeBackoffInterval(int base, int iteration) -
call
Runs the givenrunnable
at mostattemptsCount
times until it passes, leavingintervalMillis
milliseconds between the invocations. The runnable is reexecuted if it throws aRuntimeException
orAssertionError
.- Parameters:
supplier
-attemptsCount
- Total number of attempts to execute therunnable
intervalMillis
-- Returns:
- Value generated by the
supplier
.
-