public class RetryManager2 extends Object
null or throw
RetryManager2.TimeoutException depending on configuration.
The function must be a Java8 Supplier, which returns a value. Use
a lambda to wrap calls that don't return a value.
If the thread is interrupted (and the invoked operation is interruptible) then
the invocation will return null.
Instances are reusable and thread-safe, so can be created once to define the "standard" retry logic for a given operation.
| Modifier and Type | Class and Description |
|---|---|
static class |
RetryManager2.TimeoutException
This exception is thrown by the RetryManager on timeout.
|
| Constructor and Description |
|---|
RetryManager2(String operationName,
Duration initialDuration)
Convenience constructor: uses exponential backoff and throws on timeout.
|
RetryManager2(String operationName,
Duration initialDuration,
boolean isExponential,
boolean throwOnTimeout)
Base constructor.
|
| Modifier and Type | Method and Description |
|---|---|
<T> T |
invoke(Duration timeout,
Supplier<T> supplier)
Invokes the passed function, propagating exceptions.
|
<T> T |
invoke(Duration timeout,
Supplier<T> supplier,
Consumer<RuntimeException> exceptionHandler)
Invokes the passed function, passing any exceptions to the provided handler.
|
<T> T |
invoke(Instant timeoutAt,
Supplier<T> supplier)
Invokes the passed function, propagating exceptions.
|
<T> T |
invoke(Instant timeoutAt,
Supplier<T> supplier,
Consumer<RuntimeException> exceptionHandler)
Invokes the passed function, passing any exceptions to the provided handler.
|
static boolean |
sleepQuietly(long duration)
Sleeps for the specified duration (in milliseconds).
|
public RetryManager2(String operationName, Duration initialDuration, boolean isExponential, boolean throwOnTimeout)
operationName - The name of the operation, used when throwing for timeout.initialDuration - The initial sleep duration, in milliseconds.isExponential - If true, then the sleep duration is doubled for every attempt.
If false, the sleeps are of equal length.throwOnTimeout - If true, then timeout causes an exception; if false, timeout
returns null.public RetryManager2(String operationName, Duration initialDuration)
operationName - The name of the operation, used when throwing for timeout.initialDuration - The initial sleep duration, in milliseconds.public <T> T invoke(Instant timeoutAt, Supplier<T> supplier, Consumer<RuntimeException> exceptionHandler)
public <T> T invoke(Instant timeoutAt, Supplier<T> supplier)
public <T> T invoke(Duration timeout, Supplier<T> supplier, Consumer<RuntimeException> exceptionHandler)
public <T> T invoke(Duration timeout, Supplier<T> supplier)
public static boolean sleepQuietly(long duration)
true if
the sleep completes normally, false if the thread is interrupted.Copyright © 2023. All rights reserved.