Search in sources :

Example 1 with ExponentialRandomBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy in project today-infrastructure by TAKETODAY.

the class RetrySimulationTests method testSimulatorExercisesRandomExponentialBackoff.

@Test
public void testSimulatorExercisesRandomExponentialBackoff() {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);
    ExponentialBackOffPolicy backOffPolicy = new ExponentialRandomBackOffPolicy();
    backOffPolicy.setMultiplier(2);
    backOffPolicy.setMaxInterval(30000);
    backOffPolicy.setInitialInterval(100);
    RetrySimulator simulator = new RetrySimulator(backOffPolicy, retryPolicy);
    RetrySimulation simulation = simulator.executeSimulation(10000);
    System.out.println(backOffPolicy);
    System.out.println("Longest sequence  " + simulation.getLongestTotalSleepSequence());
    System.out.println("Percentiles:       " + simulation.getPercentiles());
    assertTrue(simulation.getPercentiles().size() > 4);
}
Also used : ExponentialRandomBackOffPolicy(cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 2 with ExponentialRandomBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy in project today-framework by TAKETODAY.

the class RetryTemplateBuilder method exponentialBackoff.

/**
 * Use exponential backoff policy. The formula of backoff period (without randomness):
 * <p>
 * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
 * <p>
 * (for first attempt retryNum = 0)
 *
 * @param initialInterval in milliseconds
 * @param multiplier see the formula above
 * @param maxInterval in milliseconds
 * @param withRandom adds some randomness to backoff intervals. For details, see
 * {@link ExponentialRandomBackOffPolicy}
 * @return this
 * @see ExponentialBackOffPolicy
 * @see ExponentialRandomBackOffPolicy
 */
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom) {
    Assert.isNull(this.backOffPolicy, "You have already selected backoff policy");
    Assert.isTrue(initialInterval >= 1, "Initial interval should be >= 1");
    Assert.isTrue(multiplier > 1, "Multiplier should be > 1");
    Assert.isTrue(maxInterval > initialInterval, "Max interval should be > than initial interval");
    ExponentialBackOffPolicy policy = withRandom ? new ExponentialRandomBackOffPolicy() : new ExponentialBackOffPolicy();
    policy.setInitialInterval(initialInterval);
    policy.setMultiplier(multiplier);
    policy.setMaxInterval(maxInterval);
    this.backOffPolicy = policy;
    return this;
}
Also used : ExponentialRandomBackOffPolicy(cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy)

Example 3 with ExponentialRandomBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy in project today-framework by TAKETODAY.

the class RetrySimulationTests method testSimulatorExercisesRandomExponentialBackoff.

@Test
public void testSimulatorExercisesRandomExponentialBackoff() {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);
    ExponentialBackOffPolicy backOffPolicy = new ExponentialRandomBackOffPolicy();
    backOffPolicy.setMultiplier(2);
    backOffPolicy.setMaxInterval(30000);
    backOffPolicy.setInitialInterval(100);
    RetrySimulator simulator = new RetrySimulator(backOffPolicy, retryPolicy);
    RetrySimulation simulation = simulator.executeSimulation(10000);
    System.out.println(backOffPolicy);
    System.out.println("Longest sequence  " + simulation.getLongestTotalSleepSequence());
    System.out.println("Percentiles:       " + simulation.getPercentiles());
    assertTrue(simulation.getPercentiles().size() > 4);
}
Also used : ExponentialRandomBackOffPolicy(cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Test(org.junit.Test)

Example 4 with ExponentialRandomBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy in project today-infrastructure by TAKETODAY.

the class RetryTemplateBuilder method exponentialBackoff.

/**
 * Use exponential backoff policy. The formula of backoff period (without randomness):
 * <p>
 * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
 * <p>
 * (for first attempt retryNum = 0)
 *
 * @param initialInterval in milliseconds
 * @param multiplier see the formula above
 * @param maxInterval in milliseconds
 * @param withRandom adds some randomness to backoff intervals. For details, see
 * {@link ExponentialRandomBackOffPolicy}
 * @return this
 * @see ExponentialBackOffPolicy
 * @see ExponentialRandomBackOffPolicy
 */
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom) {
    Assert.isNull(this.backOffPolicy, "You have already selected backoff policy");
    Assert.isTrue(initialInterval >= 1, "Initial interval should be >= 1");
    Assert.isTrue(multiplier > 1, "Multiplier should be > 1");
    Assert.isTrue(maxInterval > initialInterval, "Max interval should be > than initial interval");
    ExponentialBackOffPolicy policy = withRandom ? new ExponentialRandomBackOffPolicy() : new ExponentialBackOffPolicy();
    policy.setInitialInterval(initialInterval);
    policy.setMultiplier(multiplier);
    policy.setMaxInterval(maxInterval);
    this.backOffPolicy = policy;
    return this;
}
Also used : ExponentialRandomBackOffPolicy(cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy)

Aggregations

ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)4 ExponentialRandomBackOffPolicy (cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy)4 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)2 Test (org.junit.Test)2