@FunctionalInterface public interface RetryPredicate extends Predicate<RetryContext>
Modifier and Type | Method and Description |
---|---|
static RetryPredicate |
ofAlwaysTrue()
Creates a predicate that yields always true.
|
static RetryPredicate |
ofMatchingErrors(Class<? extends Throwable> errorClass)
Creates a predicate which will be true if the latest error is a subclass of
errorClass . |
static RetryPredicate |
ofMatchingStatusCodes(int first,
int... more)
Creates a predicate which matches specific status codes for
SphereServiceException s. |
static RetryPredicate |
ofMatchingStatusCodes(Predicate<Integer> predicate)
Creates a predicate which matches another predicate for status codes for
SphereServiceException s. |
boolean |
test(RetryContext retryContext) |
boolean test(RetryContext retryContext)
test
in interface Predicate<RetryContext>
static RetryPredicate ofMatchingErrors(Class<? extends Throwable> errorClass)
errorClass
.errorClass
- error class to matchstatic RetryPredicate ofAlwaysTrue()
static RetryPredicate ofMatchingStatusCodes(int first, int... more)
SphereServiceException
s.
public class RetryBadGatewayExample {
public static SphereClient ofRetry(final SphereClient delegate) {
final int maxAttempts = 5;
final List<RetryRule> retryRules = singletonList(RetryRule.of(
RetryPredicate.ofMatchingStatusCodes(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504),
RetryAction.ofScheduledRetry(maxAttempts, context -> Duration.ofSeconds(context.getAttempt() * 2)))
);
return RetrySphereClientDecorator.of(delegate, retryRules);
}
}
See the test code.
first
- the mandatory status code which might matchmore
- varargs parameter for more status codes to matchstatic RetryPredicate ofMatchingStatusCodes(Predicate<Integer> predicate)
SphereServiceException
s.predicate
- predicate which tests if a status code should match the RetryPredicate