Java Executor
that makes it easy to have tasks retry if they fail
One use case for this is to deal with rate limited APIs were you might need to have your request back-off for a while until the limit is up
- Create a
RetryExecutor
RetryExecutor retryExecutor = new RetryExecutor(executorService);
It delegates to theScheduledExecutorService
you pass it to run its task - Create a
Runnable
and annotate with the desiredRetryStrategy
A working implementation is shown in the unit tests in the source.@RetryWithFixedDelay(delay = 1, delayUnit = TimeUnit.SECONDS, maxTries = 10) class MyRunnable implements Runnable { } // or... @RetryWithExponentialDelay(multiplier = 2, initialDelay = 1, initialDelayUnit = TimeUnit.SECONDS, maxTries = 5) class MyRunnable implements Runnable { } // or... @RetryWithCustomDelay(retryStrategy = MyCustomRetryStrategyImpl.class) class MyRunnable implements Runnable {
- Run the
Runnable
on theRetryExecutor
and it will execute according to the policy noted in the annotation TheRetryExecutor
also lets you run an unannotatedRunnable
s as well
Can include this in your project using JitPack https://jitpack.io/docs/#building-with-jitpack