Again
is a simple Java retry library which only needs Java 8+ version.
Firstly, you should add latest Again
dependency to your project.
<dependency>
<groupId>io.github.ufukhalis</groupId>
<artifactId>again</artifactId>
<version>0.0.3</version>
</dependency>
Then you need to make configuration for your retry like below
RetryConfig retryConfig = new RetryConfig.RetryConfigBuilder()
.withRetryCount(3)
.withPolicy(new ExactWaitingPolicy(Duration.ofMillis(100)))
.build();
You can choose also Exponential
policy.
new ExponentialWaitingPolicy((Duration.ofMillis(100))
After that you can create your retry operation like below.
Supplier<Integer> operation = () -> 100;
Optional<Integer> maybeResult = Again.of(retryConfig, operation)
.retry();
With above configuration, it will be retried based on any exception from calling your operation
method.
So, you can also pass specific exceptions to retry
method like below.
Optional<Integer> maybeResult = Again.of(retryConfig, operation)
.retry(RuntimeException.class);
In that case, it will be retried for only specified exceptions otherwise that it won't be retried.
Besides, the exception based retry that you can also use the conditions like below.
Optional<Integer> maybeResult = Again.of(retryConfig, operation)
.withCondition(__ -> __ == 500)
.doOnEachRetry(t -> log.info(t))
.retry();
In that case, it will be retried also if value is 500
like defined above.
All code in this repository is licensed under the Apache License, Version 2.0. See LICENCE.