@FunctionalInterface public interface RetryAction extends Function<RetryContext,RetryStrategy>
RetryRule.apply(RetryContext)
.Modifier and Type | Method and Description |
---|---|
RetryStrategy |
apply(RetryContext retryContext) |
static Duration |
calculateDurationWithExponentialRandomBackoff(long retryAttempt,
long initialRetryDelay,
long maxDelay)
Computes a exponential backoff time delay in milliseconds to be used in retries, the delay grows with failed
retry attempts count with a randomness interval.
|
static RetryAction |
ofExponentialBackoff(long maxAttempts,
long initialRetryDelay,
long maxDelay)
Retry in the future with exponential backoff strategy.
|
static RetryAction |
ofGiveUpAndSendFirstException() |
static RetryAction |
ofGiveUpAndSendLatestException()
Throws the latest error and does not retry.
|
static RetryAction |
ofImmediateRetries(long maxAttempts)
Retries immediately for a maximum amount of attempts.
|
static RetryAction |
ofScheduledRetry(long maxAttempts,
Duration duration)
Retry in the future with a fixed waiting time.
|
static RetryAction |
ofScheduledRetry(long maxAttempts,
Function<RetryContext,Duration> durationFunction)
Retry in the future with a waiting time depending on the
RetryContext , for example to take the number of
attempts in consideration. |
static RetryAction |
ofShutdownServiceAndSendFirstException() |
static RetryAction |
ofShutdownServiceAndSendLatestException() |
@Nullable RetryStrategy apply(RetryContext retryContext)
apply
in interface Function<RetryContext,RetryStrategy>
static RetryAction ofScheduledRetry(long maxAttempts, Duration duration)
maxAttempts
- maximum amount of attempts until giving up and throwing the latest Exceptionduration
- time to wait before retryingstatic RetryAction ofScheduledRetry(long maxAttempts, Function<RetryContext,Duration> durationFunction)
RetryContext
, for example to take the number of
attempts in consideration.
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.
maxAttempts
- maximum amount of attempts until giving up and throwing the latest ExceptiondurationFunction
- function that takes the RetryContext
and provides the duration to wait until the next retrystatic RetryAction ofShutdownServiceAndSendFirstException()
static RetryAction ofShutdownServiceAndSendLatestException()
static RetryAction ofGiveUpAndSendFirstException()
static RetryAction ofGiveUpAndSendLatestException()
static RetryAction ofImmediateRetries(long maxAttempts)
maxAttempts
- maximum amount of attempts until giving up and throwing the latest Exceptionstatic RetryAction ofExponentialBackoff(long maxAttempts, long initialRetryDelay, long maxDelay)
maxAttempts
- maximum amount of attempts until giving up and throwing the latest ExceptioninitialRetryDelay
- initial time to wait before retrying in millisecondsmaxDelay
- maximum time to wait before retrying in millisecondsstatic Duration calculateDurationWithExponentialRandomBackoff(long retryAttempt, long initialRetryDelay, long maxDelay)
retryAttempt
- the number of attempts already tried by the client.initialRetryDelay
- the initial Retry delay.maxDelay
- the maxDelay in milliseconds.