/** * Returns a new instance of {@link org.springframework.retry.backoff.BackOffContext}, * seeded with this policies settings. */ public BackOffContext start(RetryContext context) { return new ExponentialRandomBackOffContext(getInitialInterval(), getMultiplier(), getMaxInterval()); }
/** * Pause for the {@link #setBackOffPeriod(long)}. * @throws BackOffInterruptedException if interrupted during sleep. */ protected void doBackOff() throws BackOffInterruptedException { try { sleeper.sleep(backOffPeriod); } catch (InterruptedException e) { throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } }
public FixedBackOffPolicy withSleeper(Sleeper sleeper) { FixedBackOffPolicy res = new FixedBackOffPolicy(); res.setBackOffPeriod(backOffPeriod); res.setSleeper(sleeper); return res; }
ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy(); if (backoff.random()) { policy = new ExponentialRandomBackOffPolicy(); policy.setInitialInterval(min); policy.setMultiplier(multiplier); policy.setMaxInterval(max > min ? max : ExponentialBackOffPolicy.DEFAULT_MAX_INTERVAL); if (this.sleeper != null) { policy.setSleeper(this.sleeper); UniformRandomBackOffPolicy policy = new UniformRandomBackOffPolicy(); policy.setMinBackOffPeriod(min); policy.setMaxBackOffPeriod(max); if (this.sleeper != null) { policy.setSleeper(this.sleeper); FixedBackOffPolicy policy = new FixedBackOffPolicy(); policy.setBackOffPeriod(min); if (this.sleeper != null) { policy.setSleeper(this.sleeper);
/** * Apply the backoff options. Cannot be used if a custom retry operations, or back off * policy has been set. * @param initialInterval The initial interval. * @param multiplier The multiplier. * @param maxInterval The max interval. * @return this. */ public RetryInterceptorBuilder<T> backOffOptions(long initialInterval, double multiplier, long maxInterval) { Assert.isNull(this.retryOperations, "cannot set the back off policy when a custom retryOperations has been set"); Assert.isTrue(!this.backOffPolicySet, "cannot set the back off options when a back off policy has been set"); ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy(); policy.setInitialInterval(initialInterval); policy.setMultiplier(multiplier); policy.setMaxInterval(maxInterval); this.retryTemplate.setBackOffPolicy(policy); this.backOffOptionsSet = true; this.templateAltered = true; return this; }
protected void cloneValues(ExponentialBackOffPolicy target) { target.setInitialInterval(getInitialInterval()); target.setMaxInterval(getMaxInterval()); target.setMultiplier(getMultiplier()); target.setSleeper(sleeper); }
@Bean @ConditionalOnMissingBean(name = "statsRetryTemplate") public RetryTemplate statsRetryTemplate() { RetryTemplate retryTemplate = new RetryTemplate(); ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(3000L); backOffPolicy.setMultiplier(3); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy( this.statsProperties.getElastic().getMaxAttempts(), Collections.singletonMap(Exception.class, true)); retryTemplate.setBackOffPolicy(backOffPolicy); retryTemplate.setRetryPolicy(retryPolicy); return retryTemplate; }
protected ExponentialBackOffPolicy newInstance() { return new ExponentialBackOffPolicy(); }
breaker.setResetTimeout(getResetTimeout(circuit)); template.setRetryPolicy(breaker); template.setBackOffPolicy(new NoBackOffPolicy()); String label = circuit.label(); if (!StringUtils.hasText(label)) {
public ExponentialBackOffPolicy withSleeper(Sleeper sleeper) { ExponentialBackOffPolicy res = newInstance(); cloneValues(res); res.setSleeper(sleeper); return res; }
public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) { UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy(); res.setMinBackOffPeriod(minBackOffPeriod); res.setSleeper(sleeper); return res; }
/** * Pause for a length of time equal to ' <code>exp(backOffContext.expSeed)</code>'. */ public void backOff(BackOffContext backOffContext) throws BackOffInterruptedException { ExponentialBackOffContext context = (ExponentialBackOffContext) backOffContext; try { long sleepTime = context.getSleepAndIncrement(); if (logger.isDebugEnabled()) { logger.debug("Sleeping for " + sleepTime); } sleeper.sleep(sleepTime); } catch (InterruptedException e) { throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } }
@Override public synchronized long getSleepAndIncrement() { long next = super.getSleepAndIncrement(); next = (long) (next * (1 + r.nextFloat() * (getMultiplier() - 1))); return next; }
/** * Delegates directly to the {@link #doBackOff()} method without passing on * the {@link BackOffContext} argument which is not needed for stateless * implementations. */ public final void backOff(BackOffContext backOffContext) throws BackOffInterruptedException { doBackOff(); }
public synchronized long getSleepAndIncrement() { long sleep = this.interval; if (sleep > maxInterval) { sleep = maxInterval; } else { this.interval = getNextInterval(); } return sleep; }
/** * Returns a new instance of {@link BackOffContext} configured with the 'expSeed' and * 'increment' values. */ public BackOffContext start(RetryContext context) { return new ExponentialBackOffContext(this.initialInterval, this.multiplier, this.maxInterval); }
protected ExponentialBackOffPolicy newInstance() { return new ExponentialRandomBackOffPolicy(); }
/** * Execute a single simulation * @return The sleeps which occurred within the single simulation. */ public List<Long> executeSingleSimulation() { StealingSleeper stealingSleeper = new StealingSleeper(); SleepingBackOffPolicy<?> stealingBackoff = backOffPolicy.withSleeper(stealingSleeper); RetryTemplate template = new RetryTemplate(); template.setBackOffPolicy(stealingBackoff); template.setRetryPolicy(retryPolicy); try { template.execute(new FailingRetryCallback()); } catch(FailingRetryException e) { } catch(Throwable e) { throw new RuntimeException("Unexpected exception", e); } return stealingSleeper.getSleeps(); }
/** * Pause for the {@link #setMinBackOffPeriod(long)}. * @throws BackOffInterruptedException if interrupted during sleep. */ protected void doBackOff() throws BackOffInterruptedException { try { long delta = maxBackOffPeriod==minBackOffPeriod ? 0 : random.nextInt((int) (maxBackOffPeriod - minBackOffPeriod)); sleeper.sleep(minBackOffPeriod + delta ); } catch (InterruptedException e) { throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } }