/** * The error case should be dealt with giving up and throwing an exception. * * @param error the exception to throw * @return strategy */ public static RetryStrategy resume(final Throwable error) { return new RetryStrategy(StrategyType.RESUME, error, null, null); }
private <P, R> void handle(final RetryContextImpl<P, R> retryContext) { final RetryStrategy retryStrategy = applyContext(retryContext); final StrategyType strategyType = retryStrategy.getStrategyType(); if (strategyType == StrategyType.RESUME || strategyType == StrategyType.STOP) { retryContext.getResult().completeExceptionally(retryStrategy.getError()); if (strategyType == StrategyType.STOP) { closeService(retryContext); } } else { final Function<P, CompletionStage<R>> function = retryContext.getFunction(); final Object parameter = retryStrategy.getParameter(); if (strategyType == StrategyType.RETRY_IMMEDIATELY) { retry(retryContext, function, parameter); } else if (strategyType == StrategyType.RETRY_SCHEDULED) { final Duration duration = retryStrategy.getDuration(); retryContext.schedule(() -> retry(retryContext, function, parameter), duration); } else { throw new IllegalStateException("illegal state for " + retryStrategy); } } }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { if (retryOperationContext.getAttempt() > maxAttempts) { return RetryStrategy.resume(retryOperationContext.getLatestError()); } else { final Object parameterObject = retryOperationContext.getLatestParameter(); return RetryStrategy.retryImmediately(parameterObject); } }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { if (retryOperationContext.getAttempt() > maxAttempts) { return RetryStrategy.resume(retryOperationContext.getLatestError()); } else { final Duration duration = durationFunction.apply(retryOperationContext); final Object parameterObject = retryOperationContext.getLatestParameter(); return RetryStrategy.retryScheduled(parameterObject, duration); } }
@SuppressWarnings("unchecked") private static RetryAction retryWithNewVersion() { return (RetryContext c) -> { final SphereRequest sphereRequest = (SphereRequest) c.getLatestParameter(); final Object newParameter = new SphereRequestDecorator(sphereRequest) { @Override public HttpRequestIntent httpRequestIntent() { final HttpRequestIntent original = super.httpRequestIntent(); final Long currentVersion = ((ConcurrentModificationException) c.getLatestError()).getCurrentVersion(); final String path = original.getPath().replaceAll("\\bversion=\\d+", "version=" + currentVersion); return original.withPath(path); } }; return RetryStrategy.retryImmediately(newParameter); }; } }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { return RetryStrategy.resume(retryOperationContext.getFirstError()); }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { return RetryStrategy.stop(retryOperationContext.getFirstError()); }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { return RetryStrategy.resume(retryOperationContext.getLatestError()); }
@Override public RetryStrategy apply(final RetryContext retryOperationContext) { return RetryStrategy.stop(retryOperationContext.getLatestError()); }
/** * The error case should be dealt with by retrying immediately with the given parameter. * @param parameter parameter or parameter object to retry with, can be a new one or the old one * @return strategy */ public static RetryStrategy retryImmediately(final Object parameter) { return new RetryStrategy(StrategyType.RETRY_IMMEDIATELY, null, parameter, null); }
/** * The error case should be dealt with by retrying later with the given parameter. * @param parameter parameter or parameter object to retry with, can be a new one or the old one * @param duration the amount of time to wait until the next try * @return strategy */ public static RetryStrategy retryScheduled(final Object parameter, final Duration duration) { return new RetryStrategy(StrategyType.RETRY_SCHEDULED, null, parameter, duration); }
/** * The error case should cause the service to stop and throw an exception. * @param error the exception to throw * @return strategy */ public static RetryStrategy stop(final Throwable error) { return new RetryStrategy(StrategyType.STOP, error, null, null); }