public interface RetryRule
The method test(RetryContext)
checks of a rule applies and apply(RetryContext)
executes the rule.
Example which retries with a delay on gateway timeouts:
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.
Modifier and Type | Method and Description |
---|---|
RetryStrategy |
apply(RetryContext retryContext)
Applies the retry rule.
|
static RetryRule |
of(Predicate<RetryContext> matches,
Function<RetryContext,RetryStrategy> function) |
boolean |
test(RetryContext retryContext)
Tests if this rule can be applied to the context.
|
boolean test(RetryContext retryContext)
RetryPredicate
provides some standard predicates.retryContext
- the context to check if this rule can be appliedRetryStrategy apply(RetryContext retryContext)
test(RetryContext)
would yield false for retryContext
.
RetryAction
provides some standard implementations.retryContext
- static RetryRule of(Predicate<RetryContext> matches, Function<RetryContext,RetryStrategy> function)