Skip to content

akornatskyy/retry-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

retry-java

tests maven central

Repeat failed call.

Usage

Initialize the retry options to max 3 calls and a fixed backoff of 500 ms.

RetryOptions options = RetryOptions.builder()
    .max(3)
    .backoff(new FixedBackoff(Duration.ofMillis(500)))
    .build();

Initialize the retry options with ±10% jitter backoff of 2 seconds.

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.

RetryOptions options = RetryOptions.builder()
    .backoff(
        ExpBackoff.builder()
            .initial(Duration.ofSeconds(1))
            .multiplier(1.5)
            .factor(0.25)
            .build())
    .build();

Retry Runnable

RetryRunnable.run(() -> handler.operation(), options);

Runnable with predicate (repeats call for MyRuntimeException only):

RetryRunnable.run(
    () -> handler.operation(100),
    (ex) -> ex instanceof MyRuntimeException,
    options);

Retry Supplier

Response response = RetrySupplier.get(() -> handler.operation(), options);

Supplier with predicate for response value:

Response response = RetrySupplier.get(
    () -> handler.operation(100),
    (r, ex) -> r != null && r.getStatusCode() != 200,
    options);

Retry Throwing Supplier

Response response = RetryThrowingSupplier.get(
    () -> handler.operation(), options);

Retry Async Supplier

Initialize ScheduledExecutorService (used to schedule retry delay):

ScheduledExecutorService scheduledExecutor = Executors
    .newSingleThreadScheduledExecutor();
CompletableFuture<Response> future = RetryAsyncSupplier.get(
    scheduledExecutor,
    () -> handler.operation(100),
    options);

Supplier<CompletableFuture<T>> with predicate for response value:

CompletableFuture<Response> future = RetryAsyncSupplier.get(
    scheduledExecutor,
    () -> handler.operation(),
    (r, ex) -> r != null && r.getStatusCode() != 200,
    options);

Install

Add as a maven dependency:

<dependency>
  <groupId>io.github.akornatskyy</groupId>
  <artifactId>retry</artifactId>
  <version>1.0.0</version>
</dependency>

Release

mvn versions:set -DnewVersion=1.X.0
mvn -P release clean deploy

About

🔁 Repeat failed call

Topics

Resources

License

Stars

Watchers

Forks

Languages