https://github.com/akornatskyy/retry-java
:repeat: Repeat failed call
https://github.com/akornatskyy/retry-java
backoff resilience
Last synced: about 1 month ago
JSON representation
:repeat: Repeat failed call
- Host: GitHub
- URL: https://github.com/akornatskyy/retry-java
- Owner: akornatskyy
- License: mit
- Created: 2022-05-03T10:17:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-09T09:00:18.000Z (about 1 year ago)
- Last Synced: 2025-03-09T09:27:11.235Z (about 1 year ago)
- Topics: backoff, resilience
- Language: Java
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# retry-java
[](https://github.com/akornatskyy/retry-java/actions/workflows/tests.yaml)
[](https://central.sonatype.com/artifact/io.github.akornatskyy/retry)
Repeat failed call.
## Usage
Initialize the retry options to max 3 calls and a fixed backoff of 500 ms.
```java
RetryOptions options = RetryOptions.builder()
.max(3)
.backoff(new FixedBackoff(Duration.ofMillis(500)))
.build();
```
Initialize the retry options with ±10% jitter backoff of 2 seconds.
```java
RetryOptions options = RetryOptions.builder()
.backoff(new JitterBackoff(Duration.ofSeconds(2), 0.1))
.build();
```
Initialize the retry options with exponential backoff starting at 1 second and
growing by 1.5 with ±25% jitter.
```java
RetryOptions options = RetryOptions.builder()
.backoff(
ExpBackoff.builder()
.initial(Duration.ofSeconds(1))
.multiplier(1.5)
.factor(0.25)
.build())
.build();
```
### Retry Runnable
```java
RetryRunnable.run(() -> handler.operation(), options);
```
[Runnable](https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html)
with predicate (repeats call for `MyRuntimeException` only):
```java
RetryRunnable.run(
() -> handler.operation(100),
(ex) -> ex instanceof MyRuntimeException,
options);
```
### Retry Supplier
```java
Response response = RetrySupplier.get(() -> handler.operation(), options);
```
[Supplier](https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html)
with predicate for response value:
```java
Response response = RetrySupplier.get(
() -> handler.operation(100),
(r, ex) -> r != null && r.getStatusCode() != 200,
options);
```
### Retry Throwing Supplier
```java
Response response = RetryThrowingSupplier.get(
() -> handler.operation(), options);
```
### Retry Async Supplier
Initialize `ScheduledExecutorService` (used to schedule retry delay):
```java
ScheduledExecutorService scheduledExecutor = Executors
.newSingleThreadScheduledExecutor();
```
```java
CompletableFuture future = RetryAsyncSupplier.get(
scheduledExecutor,
() -> handler.operation(100),
options);
```
`Supplier>` with predicate for response value:
```java
CompletableFuture future = RetryAsyncSupplier.get(
scheduledExecutor,
() -> handler.operation(),
(r, ex) -> r != null && r.getStatusCode() != 200,
options);
```
## Install
Add as a maven dependency:
```xml
io.github.akornatskyy
retry
1.0.0
```
## Release
```sh
mvn versions:set -DnewVersion=1.X.0
mvn -P release clean deploy
```