/** * Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for results * that equal the {@code result}. * * @param delayFunction the function to use to compute the delay before a next attempt * @param result the execution result that is expected in order to trigger the delay * @throws NullPointerException if {@code delayFunction} or {@code result} are null * @see DelayFunction */ public RetryPolicy<R> withDelayWhen(DelayFunction<R, ? extends Throwable> delayFunction, R result) { withDelay(delayFunction); Assert.notNull(result, "result"); this.delayResult = result; return this; }
/** * Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for failures * that are assignable from the {@code failure}. * * @param delayFunction the function to use to compute the delay before a next attempt * @param failure the execution failure that is expected in order to trigger the delay * @param <F> failure type * @throws NullPointerException if {@code delayFunction} or {@code failure} are null * @see DelayFunction */ public <F extends Throwable> RetryPolicy<R> withDelayOn(DelayFunction<R, F> delayFunction, Class<F> failure) { withDelay(delayFunction); Assert.notNull(failure, "failure"); this.delayFailure = failure; return this; }
TopicMetadataLoader(BrokerMessageProducer brokerMessageProducer, int retryCount, long retryInterval, int threadPoolSize) { this.brokerMessageProducer = brokerMessageProducer; ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("topic-metadata-loader-%d").build(); this.scheduler = Executors.newScheduledThreadPool(threadPoolSize, threadFactory); this.retryPolicy = new RetryPolicy() .withMaxRetries(retryCount) .withDelay(retryInterval, TimeUnit.MILLISECONDS) .retryIf(MetadataLoadingResult::isFailure); }
protected RetryPolicy buildRetryPolicy() { RetryPolicy retryPolicy = new RetryPolicy().withMaxRetries(0); Properties userConfig = configurationGenerator.getUserConfig(); if (Boolean.parseBoolean((userConfig.getProperty(PARAM_RETRY_POLICY_ENABLED, "false")))) { int maxRetries = Integer.parseInt( userConfig.getProperty(PARAM_RETRY_POLICY_MAX_RETRIES, PARAM_RETRY_POLICY_DEFAULT_RETRIES)); int delay = Integer.parseInt( userConfig.getProperty(PARAM_RETRY_POLICY_DELAY_IN_MS, PARAM_POLICY_DEFAULT_DELAY_IN_MS)); retryPolicy = retryPolicy.retryOn(ConfigurationException.class).withMaxRetries(maxRetries).withDelay(delay, TimeUnit.MILLISECONDS); } return retryPolicy; }
public static RetryPolicy greedyRetryPolicy(long delayMs) { return new RetryPolicy() .retryOn(Arrays.asList( JedisException.class, DynoException.class, DynomiteClientDelegate.ClientDelegateException.class )) .withDelay(delayMs, TimeUnit.MILLISECONDS) .withMaxRetries(3); } }
public static RetryPolicy createRetryPolicy(final RiptideProperties.Retry config) { final RetryPolicy policy = new RetryPolicy(); Optional.ofNullable(config.getFixedDelay()) .ifPresent(delay -> delay.applyTo(policy::withDelay)); Optional.ofNullable(config.getBackoff()) .ifPresent(backoff -> { final TimeSpan delay = backoff.getDelay(); final TimeSpan maxDelay = backoff.getMaxDelay(); final TimeUnit unit = MILLISECONDS; @Nullable final Double delayFactor = backoff.getDelayFactor(); if (delayFactor == null) { policy.withBackoff(delay.to(unit), maxDelay.to(unit), unit); } else { policy.withBackoff(delay.to(unit), maxDelay.to(unit), unit, delayFactor); } }); Optional.ofNullable(config.getMaxRetries()) .ifPresent(policy::withMaxRetries); Optional.ofNullable(config.getMaxDuration()) .ifPresent(duration -> duration.applyTo(policy::withMaxDuration)); Optional.ofNullable(config.getJitterFactor()) .ifPresent(policy::withJitter); Optional.ofNullable(config.getJitter()) .ifPresent(jitter -> jitter.applyTo(policy::withJitter)); policy.retryOn(TransientFaultException.class); policy.retryOn(RetryException.class); policy.withDelay(new RetryAfterDelayFunction(systemUTC())); return policy; }
private void setPassword(String rootUrl) throws IOException, ClientProtocolException { HttpClient client = createClient("neo4j"); RetryPolicy retryPolicy = new RetryPolicy() .retryOn(NoHttpResponseException.class) .withDelay(1, TimeUnit.SECONDS) .withMaxRetries(3); Failsafe.with(retryPolicy).run(() -> callSetPassword(rootUrl, client)); }
/** * Pull the given virtual resource. * * @param virtualResource the virtual resource * @param image the image */ void pullImage(VirtualResource virtualResource, String image) { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withDelay(virtualResource.delay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()); Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.warn( "Retrying pull request of image '{}'", image, throwable)) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Image image '{}' could not be pulled: ", image, throwable)) .run(() -> client.pull(image, new AnsiProgressHandler())); }