Class Retry
java.lang.Object
dev.fforj.Retry
Retry policies for fallible operations.
Designed to be called from a virtual-thread context: Thread.sleep(Duration)
is safe and cheap on virtual threads; no executor / scheduled task overhead.
Cancellation is structural: if the calling thread is interrupted (e.g. because a
surrounding structured-concurrency scope shut down), the sleep throws
InterruptedException and the retry loop bails immediately.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <E,T> Result <E, T> run(Retry.Policy policy, Predicate<? super E> shouldRetry, IntFunction<? extends Result<E, T>> body) Runbodyup toRetry.Policy.maxAttemptstimes until it succeeds, the error fails theshouldRetrypredicate, or the caller's thread is interrupted.static <E,T> Result <E, T> run(Retry.Policy policy, Predicate<? super E> shouldRetry, Supplier<? extends Result<E, T>> body) run, for bodies that don't care which attempt they're on.
-
Method Details
-
run
public static <E,T> Result<E,T> run(Retry.Policy policy, Predicate<? super E> shouldRetry, IntFunction<? extends Result<E, T>> body) throws InterruptedExceptionRunbodyup toRetry.Policy.maxAttemptstimes until it succeeds, the error fails theshouldRetrypredicate, or the caller's thread is interrupted.The body receives the current attempt number, starting at
1. The retry loop owns that counter, so callers never track it in outside mutable state. Use it for logging ("attempt 3 of 5 failed"), attempt-dependent behavior, or building it into the success value. When the number doesn't matter, use theSupplier overload.- Parameters:
policy- attempts, delay, backoff.shouldRetry- predicate over the error. Returntrueto retry on this error;falseto surface immediately. Use this to distinguish transient failures (rate-limit, 5xx) from terminal ones (4xx, validation).body- the operation to run, given the 1-based attempt number. Must be effectively idempotent.- Returns:
- the last
Resultproduced. AlwaysOkon success; the most recentErron exhaustion. - Throws:
InterruptedException- if the calling thread is interrupted during sleep.
-
run
public static <E,T> Result<E,T> run(Retry.Policy policy, Predicate<? super E> shouldRetry, Supplier<? extends Result<E, T>> body) throws InterruptedExceptionrun, for bodies that don't care which attempt they're on.- Throws:
InterruptedException
-