public interface RetryPolicy
Implementors should define their own exhaustion policy, i.e. when they will give up retrying. Implementors should track retry attempts, even if they don't leverage it when deciding exhaustion. Exampled of other uses are logging or to customize delays. Implementors should also define what kind of delay will be executed between retries.
and its implementors for different strategies.
{@code
import ratpack.exec.ExecResult;
import ratpack.exec.Promise;
import ratpack.exec.util.retry.AttemptRetryPolicy;
import ratpack.exec.util.retry.RetryPolicy;
import ratpack.exec.util.retry.FixedDelay;
import ratpack.test.exec.ExecHarness;
import java.time.Duration;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
public class Example {
private static final List LOG = new LinkedList<>();
public static void main(String... args) throws Exception {
AtomicInteger source = new AtomicInteger();
RetryPolicy retryPolicy = AttemptRetryPolicy.of(b -> b
.delay(FixedDelay.of(Duration.ofMillis(500)))
.maxAttempts(3));
ExecResult result = ExecHarness.yieldSingle(exec ->
Promise.sync(source::incrementAndGet)
.mapIf(i -> i < 3, i -> { throw new IllegalStateException(); })
.retry(retryPolicy, (i, t) -> LOG.add("retry attempt: " + i))
);
assertEquals(Integer.valueOf(3), result.getValue());
assertEquals(Arrays.asList("retry attempt: 1", "retry attempt: 2"), LOG);
}
}
}
,
Promise.retry(RetryPolicy, ratpack.func.BiAction)
Modifier and Type | Method and Description |
---|---|
int |
attempts()
Attempts performed so far.
|
Promise<java.time.Duration> |
delay()
Promise that returns the waiting time before retrying.
|
RetryPolicy |
increaseAttempt()
Increase number of attempts.
|
boolean |
isExhausted()
If the caller should stop retrying.
|
boolean isExhausted()
int attempts()
RetryPolicy increaseAttempt()
Promise<java.time.Duration> delay()
and its implementors for different strategies