Package ratpack.exec.util.retry
Class DurationRetryPolicy
- java.lang.Object
-
- ratpack.exec.util.retry.DurationRetryPolicy
-
- All Implemented Interfaces:
RetryPolicy
public class DurationRetryPolicy extends java.lang.Object implements RetryPolicy
A duration based implementation ofRetryPolicy.This strategy will signal end of retries when the elapsed time from the first error occurrence surpasses the configurable max duration, 30 seconds by default.
import ratpack.exec.ExecResult; import ratpack.exec.Promise; import ratpack.exec.util.retry.DurationRetryPolicy; import ratpack.exec.util.retry.RetryPolicy; import ratpack.exec.util.retry.FixedDelay; import ratpack.test.exec.ExecHarness; import ratpack.test.internal.time.FixedWindableClock; import java.time.Duration; import java.time.Instant; import java.time.ZoneOffset; 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<String> LOG = new LinkedList<>(); public static void main(String... args) throws Exception { AtomicInteger source = new AtomicInteger(); FixedWindableClock clock = new FixedWindableClock(Instant.now(), ZoneOffset.UTC); RetryPolicy retryPolicy = DurationRetryPolicy.of(b -> b .delay(FixedDelay.of(Duration.ofMillis(500))) .maxDuration(Duration.ofSeconds(10)) .clock(clock)); RuntimeException e = new RuntimeException("!"); Throwable result = ExecHarness.yieldSingle(exec -> Promise.sync(source::incrementAndGet) .mapIf(i -> i == 3, i -> { clock.windClock(Duration.ofMinutes(5)); return i; }) .map(i -> { throw new IllegalStateException(); }) .retry(retryPolicy, (i, t) -> LOG.add("retry attempt: " + i)) ).getThrowable(); assertEquals("java.lang.IllegalStateException", result.getClass().getCanonicalName()); assertEquals(Arrays.asList("retry attempt: 1", "retry attempt: 2"), LOG); } }- Since:
- 1.7
- See Also:
DurationRetryPolicyBuilder
-
-
Constructor Summary
Constructors Constructor Description DurationRetryPolicy(Delay delay, java.time.Duration maxDuration, java.time.Clock clock)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intattempts()Attempts performed so far.Promise<java.time.Duration>delay()Promise that returns the waiting time before retrying.RetryPolicyincreaseAttempt()Increase number of attempts.booleanisExhausted()If the caller should stop retrying.static DurationRetryPolicyof(Action<? super DurationRetryPolicyBuilder> definition)Builds a new duration based retry policy from the given definition.
-
-
-
Constructor Detail
-
DurationRetryPolicy
public DurationRetryPolicy(Delay delay, java.time.Duration maxDuration, java.time.Clock clock)
-
-
Method Detail
-
of
public static DurationRetryPolicy of(Action<? super DurationRetryPolicyBuilder> definition) throws java.lang.Exception
Builds a new duration based retry policy from the given definition.- Parameters:
definition- the duration based retry policy definition- Returns:
- a duration based retry policy
- Throws:
java.lang.Exception- any thrown by building the duration based retry policy
-
isExhausted
public boolean isExhausted()
If the caller should stop retrying.- Specified by:
isExhaustedin interfaceRetryPolicy- Returns:
- TRUE if the caller should stop retrying
-
attempts
public int attempts()
Attempts performed so far. Starts on 1, i.e. when no retry has been performed yet this returns 1.- Specified by:
attemptsin interfaceRetryPolicy- Returns:
- attempts performed so far.
-
delay
public Promise<java.time.Duration> delay()
Promise that returns the waiting time before retrying.- Specified by:
delayin interfaceRetryPolicy- Returns:
- promise that returns the waiting time before retrying
- See Also:
and its implementors for different strategies
-
increaseAttempt
public RetryPolicy increaseAttempt()
Increase number of attempts.- Specified by:
increaseAttemptin interfaceRetryPolicy- Returns:
- this policy after updating the internal state around attempts
-
-