Tabnine Logo
RetryPredicate
Code IndexAdd Tabnine to your IDE (free)

How to use
RetryPredicate
in
io.sphere.sdk.retry

Best Java code snippets using io.sphere.sdk.retry.RetryPredicate (Showing top 12 results out of 315)

origin: commercetools/commercetools-jvm-sdk

private RetryStrategy applyContext(final RetryContext retryContext) {
  final Optional<RetryRule> matchingRetryRuleOption = findMatchingRetryRule(retryRules, retryContext);
  final RetryRule matchingRetryRule = matchingRetryRuleOption
      .orElseGet(() -> RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofGiveUpAndSendLatestException()));
  return matchingRetryRule.apply(retryContext);
}
origin: commercetools/commercetools-jvm-sdk

@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();
}
origin: commercetools/commercetools-jvm-sdk

/**
 * 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);
}
origin: commercetools/commercetools-jvm-sdk

  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);
  }
}
origin: com.commercetools.sdk.jvm.core/commercetools-java-client-core

private List<RetryRule> createRules() {
  final Predicate<RetryContext> isFatal = r -> {
    final Throwable latestError = r.getLatestError();
    final boolean unknownHost = latestError instanceof HttpException && latestError != null && latestError instanceof UnknownHostException;
    final boolean unauthorized = latestError instanceof UnauthorizedException;
    return unknownHost || unauthorized;
  };
  final RetryRule fatalRetryRule = RetryRule.of(isFatal, RetryAction.ofShutdownServiceAndSendLatestException());
  final RetryRule retryScheduledRetryRule = RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofScheduledRetry(2, retryContext -> Duration.ofMillis(retryContext.getAttempt() * retryContext.getAttempt() * 50)));
  return asList(fatalRetryRule, retryScheduledRetryRule);
}
origin: commercetools/commercetools-jvm-sdk

@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();
}
origin: commercetools/commercetools-jvm-sdk

private List<RetryRule> createRules() {
  final Predicate<RetryContext> isFatal = r -> {
    final Throwable latestError = r.getLatestError();
    final boolean unknownHost = latestError instanceof HttpException && latestError != null && latestError instanceof UnknownHostException;
    final boolean unauthorized = latestError instanceof UnauthorizedException;
    return unknownHost || unauthorized;
  };
  final RetryRule fatalRetryRule = RetryRule.of(isFatal, RetryAction.ofShutdownServiceAndSendLatestException());
  final RetryRule retryScheduledRetryRule = RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofScheduledRetry(2, retryContext -> Duration.ofMillis(retryContext.getAttempt() * retryContext.getAttempt() * 50)));
  return asList(fatalRetryRule, retryScheduledRetryRule);
}
origin: commercetools/commercetools-jvm-sdk

@Test
public void immediateRetryRule() throws Exception {
  try (final Service service = new Failing2TimesServiceImpl()) {
    final List<RetryRule> retryRules = singletonList(RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofImmediateRetries(3)));
    try(final AsyncRetrySupervisor supervisor = AsyncRetrySupervisor.of(retryRules)) {
      final CompletionStage<Integer> bar = supervisor.supervise(service, service::apply, "bar");
      assertThat(waitAndGet(bar)).isEqualTo(3);
      assertThat(service.isClosed()).isFalse();
    }
  }
}
origin: commercetools/commercetools-jvm-sdk

@Test
public void giveUpAndSendFirstException() throws Exception {
  try (final Service service = new Failing2TimesServiceImpl()) {
    final List<RetryRule> retryRules = singletonList(RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofGiveUpAndSendFirstException()));
    try(final AsyncRetrySupervisor supervisor = AsyncRetrySupervisor.of(retryRules)) {
      final CompletionStage<Integer> bar = supervisor.supervise(service, service::apply, "bar");
      final Throwable throwable = catchThrowable(() -> waitAndGet(bar));
      assertThat(throwable.getCause()).hasMessage(Failing2TimesServiceImpl.ERROR_MESSAGE);
      assertThat(service.isClosed()).isFalse();
    }
  }
}
origin: commercetools/commercetools-jvm-sdk

@Test
public void scheduledRetry() throws Exception {
  try (final Service service = new Failing2TimesServiceImpl()) {
    final RetryAction op = RetryAction.ofScheduledRetry(3, o -> Duration.ofMillis(o.getAttempt() * 100));
    final List<RetryRule> retryRules = singletonList(RetryRule.of(RetryPredicate.ofAlwaysTrue(), op));
    try(final AsyncRetrySupervisor supervisor = AsyncRetrySupervisor.of(retryRules)) {
      final CompletionStage<Integer> bar = supervisor.supervise(service, service::apply, "bar");
      assertThat(waitAndGet(bar)).isEqualTo(3);
      assertThat(service.isClosed()).isFalse();
    }
  }
}
origin: commercetools/commercetools-jvm-sdk

@Test
public void shutdownServiceAndSendFirstException() throws Exception {
  try (final Service service = new Failing2TimesServiceImpl()) {
    final List<RetryRule> retryRules = singletonList(RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofShutdownServiceAndSendFirstException()));
    try(final AsyncRetrySupervisor supervisor = AsyncRetrySupervisor.of(retryRules)) {
      final CompletionStage<Integer> bar = supervisor.supervise(service, service::apply, "bar");
      final Throwable throwable = catchThrowable(() -> waitAndGet(bar));
      assertThat(throwable.getCause()).hasMessage(Failing2TimesServiceImpl.ERROR_MESSAGE);
      Thread.sleep(200);
      assertThat(service.isClosed()).isTrue();
    }
  }
}
origin: commercetools/commercetools-jvm-sdk

@Test
public void immediateRetryRuleNonSufficient() throws Exception {
  try (final Service service = new Failing2TimesServiceImpl()) {
    final List<RetryRule> retryRules = singletonList(RetryRule.of(RetryPredicate.ofAlwaysTrue(), RetryAction.ofImmediateRetries(1)));
    try(final AsyncRetrySupervisor supervisor = AsyncRetrySupervisor.of(retryRules)) {
      final CompletionStage<Integer> bar = supervisor.supervise(service, service::apply, "bar");
      final Throwable throwable = catchThrowable(() -> waitAndGet(bar));
      assertThat(throwable.getCause()).hasMessage(Failing2TimesServiceImpl.ERROR_MESSAGE);
      assertThat(service.isClosed()).isFalse();
    }
  }
}
io.sphere.sdk.retryRetryPredicate

Javadoc

Predicate if a retryContext can be handled. The static factory methods provide predicates for typical use cases.

Most used methods

  • ofAlwaysTrue
  • ofMatchingStatusCodes
    Creates a predicate which matches another predicate for status codes for SphereServiceExceptions.
  • test

Popular in Java

  • Making http requests using okhttp
  • onRequestPermissionsResult (Fragment)
  • addToBackStack (FragmentTransaction)
  • getApplicationContext (Context)
  • BufferedWriter (java.io)
    Wraps an existing Writer and buffers the output. Expensive interaction with the underlying reader is
  • Iterator (java.util)
    An iterator over a sequence of objects, such as a collection.If a collection has been changed since
  • TreeSet (java.util)
    TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are support
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • TimeUnit (java.util.concurrent)
    A TimeUnit represents time durations at a given unit of granularity and provides utility methods to
  • Join (org.hibernate.mapping)
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now