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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations