This is a simple and lightweight retry library for Java. It helps you transparently retry failed operations.
<dependency>
<groupId>io.github.pepperkit</groupId>
<artifactId>retry</artifactId>
<version>1.0.0</version>
</dependency>
implementation 'io.github.pepperkit:retry:1.0.0'
The backoff function determines how much to wait between the retries.
It waits progressively longer intervals between subsequent retries.
3s -> 9s -> 27s -> 81s
import io.github.pepperkit.retry.BackoffFunction;
new BackoffFunction.Exponential(3);
This is an elementary implementation, just return a constant value.
3s -> 3s -> 3s -> 3s
import io.github.pepperkit.retry.BackoffFunction;
new BackoffFunction.Fixed();
This function returns a random interval value. The algorithm doesn't declare how much the rate of an operation will be reduced.
12s -> 4s -> 3s -> 9s
import io.github.pepperkit.retry.BackoffFunction;
new BackoffFunction.Randomized(5);
You can implement your own version of backoff function by the interface.
@FunctionalInterface
public interface BackoffFunction {
Duration delay(int attempt, Duration delay);
}
retry()
- initiates the retry functionbackoff(BackoffFunction function)
- a function to compute next delay intervaldelay(Duration duration)
- an initial delay intervalmaxDelay(Duration duration)
- the maximum delay interval valuehandle(Class<? extends Throwable>)
- specifies a type of Exception which has to be handled to retry (if it doesn't specify any exception will be handled by default)abortIf(Class<? extends Throwable>)
- when the exception has occurred the retryable function will be interruptedonFailure(Consumer<? super Throwable>)
- specifies a function to handle the exceptionrun()
- perform an action (function) which can be retriedcall()
- compute a result which can be retried
The exponential backoff algorithm implementation. It uses to gradually reduce the rate of the operation if the exception (or any) occurred.
import static io.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(new BackoffFunction.Exponential())
.delay(Duration.ofMillis(500))
.maxDelay(Duration.ofSeconds(3))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// webClient.get() throws HttpServiceUnavailable exception
// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred
// we have some assumption, this service can be available later...
});
This is a fixed backoff algorithm implementation. There each attempt of the operation will be retried by the same timeout if the exception (or any) occurred.
import static io.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(new BackoffFunction.Fixed())
.delay(Duration.ofMillis(500))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred
// we have some assumption, this service can be available later...
});
This is a randomized backoff algorithm implementation.
import static io.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(new BackoffFunction.Randomized(5))
.delay(Duration.ofMillis(500))
.maxDelay(Duration.ofSeconds(3))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred
// we have some assumption, this service can be available later...
});
The library is licensed under the terms of the MIT License.