@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
SphereServiceExceptions. |
static RetryPredicate |
ofMatchingStatusCodes(Predicate<Integer> predicate)
Creates a predicate which matches another predicate for status codes for
SphereServiceExceptions. |
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)
SphereServiceExceptions.
import io.sphere.sdk.client.RetrySphereClientDecorator;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.retry.RetryAction;
import io.sphere.sdk.retry.RetryPredicate;
import io.sphere.sdk.retry.RetryRule;
import java.time.Duration;
import java.util.List;
import static io.sphere.sdk.http.HttpStatusCode.BAD_GATEWAY_502;
import static io.sphere.sdk.http.HttpStatusCode.GATEWAY_TIMEOUT_504;
import static io.sphere.sdk.http.HttpStatusCode.SERVICE_UNAVAILABLE_503;
import static java.util.Collections.singletonList;
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)
SphereServiceExceptions.predicate - predicate which tests if a status code should match the RetryPredicate