Package org.kiwiproject.retry
Class Retryer
- java.lang.Object
-
- org.kiwiproject.retry.Retryer
-
public final class Retryer extends Object
A retryer, which executes a call, and retries it until it succeeds, or a stop strategy decides to stop retrying. A wait strategy is used to sleep between attempts. The strategy to decide if the call succeeds or not is also configurable.A retryer can also wrap the callable into a RetryerCallable, which can be submitted to an executor.
Retryer instances are better constructed with a
RetryerBuilder
. A retryer is thread-safe, provided the arguments passed to its constructor are thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Retryer.RetryerCallable<T>
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
call(Callable<T> callable)
Executes the given callable, retrying if necessary.void
run(Runnable runnable)
Executes the given runnable, retrying if necessary.<T> Retryer.RetryerCallable<T>
wrap(Callable<T> callable)
Wraps the givenCallable
in aRetryer.RetryerCallable
, which can be submitted to an executor.
-
-
-
Method Detail
-
call
public <T> T call(Callable<T> callable) throws RetryException, InterruptedException
Executes the given callable, retrying if necessary. If the retry predicate accepts the attempt, the stop strategy is used to decide if a new attempt must be made. Then the wait strategy is used to decide how much time to sleep and a new attempt is made.- Type Parameters:
T
- the return type of the Callable- Parameters:
callable
- the callable task to be executed- Returns:
- the computed result of the given callable
- Throws:
RetryException
- if all the attempts failed before the stop strategy decided to abortInterruptedException
- If this thread is interrupted. This can happen becauseThread.sleep(long)
is invoked between attempts
-
run
public void run(Runnable runnable) throws RetryException, InterruptedException
Executes the given runnable, retrying if necessary. If the retry predicate accepts the attempt, the stop strategy is used to decide if a new attempt must be made. Then the wait strategy is used to decide how much time to sleep and a new attempt is made.- Parameters:
runnable
- the runnable task to be executed- Throws:
RetryException
- if all the attempts failed before the stop strategy decided to abortInterruptedException
- If this thread is interrupted. This can happen becauseThread.sleep(long)
is invoked between attempts
-
wrap
public <T> Retryer.RetryerCallable<T> wrap(Callable<T> callable)
Wraps the givenCallable
in aRetryer.RetryerCallable
, which can be submitted to an executor. The returnedRetryer.RetryerCallable
uses thisRetryer
instance to call the givenCallable
.- Type Parameters:
T
- the return type of the Callable- Parameters:
callable
- the callable to wrap- Returns:
- a
Retryer.RetryerCallable
that behaves like the givenCallable
with retry behavior defined by thisRetryer
-
-