Search in sources :

Example 1 with ExponentialBackOffPolicy

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

the class RetryInterceptorBuilder method backOffOptions.

/**
 * 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;
}
Also used : ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy)

Example 2 with ExponentialBackOffPolicy

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

the class StatefulRetryIntegrationTests method testExponentialBackOffIsExponential.

@Test
public void testExponentialBackOffIsExponential() throws Throwable {
    ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
    policy.setInitialInterval(100);
    policy.setMultiplier(1.5);
    RetryTemplate template = new RetryTemplate();
    template.setBackOffPolicy(policy);
    final List<Long> times = new ArrayList<Long>();
    RetryState retryState = new DefaultRetryState("bar");
    for (int i = 0; i < 3; i++) {
        try {
            template.execute(new RetryCallback<String, Exception>() {

                public String doWithRetry(RetryContext context) throws Exception {
                    times.add(System.currentTimeMillis());
                    throw new Exception("Fail");
                }
            }, new RecoveryCallback<String>() {

                public String recover(RetryContext context) throws Exception {
                    return null;
                }
            }, retryState);
        } catch (Exception e) {
            assertTrue(e.getMessage().equals("Fail"));
        }
    }
    assertEquals(3, times.size());
    assertTrue(times.get(1) - times.get(0) >= 100);
    assertTrue(times.get(2) - times.get(1) >= 150);
}
Also used : ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) ArrayList(java.util.ArrayList) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryState(cn.taketoday.retry.RetryState) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Test(org.junit.Test)

Example 3 with ExponentialBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialBackOffPolicy 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 4 with ExponentialBackOffPolicy

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

the class RetryTemplateBuilderTests method testBasicCustomization.

@Test
public void testBasicCustomization() {
    RetryListener listener1 = mock(RetryListener.class);
    RetryListener listener2 = mock(RetryListener.class);
    RetryTemplate template = RetryTemplate.builder().maxAttempts(10).exponentialBackoff(99, 1.5, 1717).retryOn(IOException.class).retryOn(Collections.<Class<? extends Throwable>>singletonList(IllegalArgumentException.class)).traversingCauses().withListener(listener1).withListeners(Collections.singletonList(listener2)).build();
    PolicyTuple policyTuple = PolicyTuple.extractWithAsserts(template);
    BinaryExceptionClassifier classifier = policyTuple.exceptionClassifierRetryPolicy.getExceptionClassifier();
    Assert.assertTrue(classifier.classify(new FileNotFoundException()));
    Assert.assertTrue(classifier.classify(new IllegalArgumentException()));
    Assert.assertFalse(classifier.classify(new RuntimeException()));
    Assert.assertFalse(classifier.classify(new OutOfMemoryError()));
    Assert.assertTrue(policyTuple.baseRetryPolicy instanceof MaxAttemptsRetryPolicy);
    Assert.assertEquals(10, ((MaxAttemptsRetryPolicy) policyTuple.baseRetryPolicy).getMaxAttempts());
    List<RetryListener> listeners = Arrays.asList(getPropertyValue(template, "listeners", RetryListener[].class));
    Assert.assertEquals(2, listeners.size());
    Assert.assertTrue(listeners.contains(listener1));
    Assert.assertTrue(listeners.contains(listener2));
    Assert.assertTrue(getPropertyValue(template, "backOffPolicy") instanceof ExponentialBackOffPolicy);
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) MaxAttemptsRetryPolicy(cn.taketoday.retry.policy.MaxAttemptsRetryPolicy) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) RetryListener(cn.taketoday.retry.RetryListener) Test(org.junit.Test)

Example 5 with ExponentialBackOffPolicy

use of cn.taketoday.retry.backoff.ExponentialBackOffPolicy 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)

Aggregations

ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)14 Test (org.junit.Test)10 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)6 ExponentialRandomBackOffPolicy (cn.taketoday.retry.backoff.ExponentialRandomBackOffPolicy)4 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)4 DirectFieldAccessor (cn.taketoday.beans.DirectFieldAccessor)2 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)2 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)2 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)2 RetryContext (cn.taketoday.retry.RetryContext)2 RetryListener (cn.taketoday.retry.RetryListener)2 RetryState (cn.taketoday.retry.RetryState)2 MaxAttemptsRetryPolicy (cn.taketoday.retry.policy.MaxAttemptsRetryPolicy)2 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)2