/** * Creates a predicate which matches specific status codes for {@link SphereServiceException}s. * * {@include.example io.sphere.sdk.client.retry.RetryBadGatewayExample} * * @param first the mandatory status code which might match * @param more varargs parameter for more status codes to match * @return predicate */ static RetryPredicate ofMatchingStatusCodes(final int first, final int ... more) { final Predicate<Integer> predicate = statusCode -> IntStream.concat(IntStream.of(first), IntStream.of(more)).anyMatch(i -> i == statusCode); return ofMatchingStatusCodes(predicate); }
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); } }
@Test public void statusCodesVarargs() { final RetryPredicate predicate = RetryPredicate.ofMatchingStatusCodes(SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504); assertThat(predicate.test(getRetryContext(new GatewayTimeoutException()))).isTrue(); assertThat(predicate.test(getRetryContext(new BadRequestException("")))).isFalse(); }
@Test public void statusCodesPredicate() { final RetryPredicate predicate = RetryPredicate.ofMatchingStatusCodes(SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504); assertThat(predicate.test(getRetryContext(new GatewayTimeoutException()))).isTrue(); assertThat(predicate.test(getRetryContext(new BadRequestException("")))).isFalse(); }