use of cn.taketoday.retry.policy.MaxAttemptsRetryPolicy in project today-infrastructure by TAKETODAY.
the class RetryTemplateBuilder method build.
/* ---------------- Building -------------- */
/**
* Finish configuration and build resulting {@link RetryTemplate}. For default
* behaviour and concurrency note see class-level doc of {@link RetryTemplateBuilder}.
* The {@code retryPolicy} of the returned {@link RetryTemplate} is always an instance
* of {@link CompositeRetryPolicy}, that consists of one base policy, and of
* {@link BinaryExceptionClassifierRetryPolicy}. The motivation is: whatever base
* policy we use, exception classification is extremely recommended.
*
* @return new instance of {@link RetryTemplate}
*/
public RetryTemplate build() {
RetryTemplate retryTemplate = new RetryTemplate();
// Exception classifier
BinaryExceptionClassifier exceptionClassifier = classifierBuilder != null ? classifierBuilder.build() : BinaryExceptionClassifier.defaultClassifier();
if (this.baseRetryPolicy == null) {
this.baseRetryPolicy = new MaxAttemptsRetryPolicy();
}
CompositeRetryPolicy finalPolicy = new CompositeRetryPolicy();
finalPolicy.setPolicies(new RetryPolicy[] { baseRetryPolicy, new BinaryExceptionClassifierRetryPolicy(exceptionClassifier) });
retryTemplate.setRetryPolicy(finalPolicy);
if (this.backOffPolicy == null) {
this.backOffPolicy = new NoBackOffPolicy();
}
retryTemplate.setBackOffPolicy(this.backOffPolicy);
if (this.listeners != null) {
retryTemplate.setListeners(this.listeners.toArray(new RetryListener[0]));
}
return retryTemplate;
}
use of cn.taketoday.retry.policy.MaxAttemptsRetryPolicy in project today-infrastructure by TAKETODAY.
the class RetryTemplateBuilder method maxAttempts.
/* ---------------- Configure retry policy -------------- */
/**
* Limits maximum number of attempts to provided value.
* <p>
* Invocation of this method does not discard default exception classification rule,
* that is "retry only on {@link Exception} and it's subclasses".
*
* @param maxAttempts includes initial attempt and all retries. E.g: maxAttempts = 3
* means one initial attempt and two retries.
* @return this
* @see MaxAttemptsRetryPolicy
*/
public RetryTemplateBuilder maxAttempts(int maxAttempts) {
Assert.isTrue(maxAttempts > 0, "Number of attempts should be positive");
Assert.isNull(this.baseRetryPolicy, "You have already selected another retry policy");
this.baseRetryPolicy = new MaxAttemptsRetryPolicy(maxAttempts);
return this;
}
use of cn.taketoday.retry.policy.MaxAttemptsRetryPolicy in project today-framework by TAKETODAY.
the class RetryTemplateBuilder method maxAttempts.
/* ---------------- Configure retry policy -------------- */
/**
* Limits maximum number of attempts to provided value.
* <p>
* Invocation of this method does not discard default exception classification rule,
* that is "retry only on {@link Exception} and it's subclasses".
*
* @param maxAttempts includes initial attempt and all retries. E.g: maxAttempts = 3
* means one initial attempt and two retries.
* @return this
* @see MaxAttemptsRetryPolicy
*/
public RetryTemplateBuilder maxAttempts(int maxAttempts) {
Assert.isTrue(maxAttempts > 0, "Number of attempts should be positive");
Assert.isNull(this.baseRetryPolicy, "You have already selected another retry policy");
this.baseRetryPolicy = new MaxAttemptsRetryPolicy(maxAttempts);
return this;
}
use of cn.taketoday.retry.policy.MaxAttemptsRetryPolicy in project today-framework by TAKETODAY.
the class RetryTemplateBuilder method build.
/* ---------------- Building -------------- */
/**
* Finish configuration and build resulting {@link RetryTemplate}. For default
* behaviour and concurrency note see class-level doc of {@link RetryTemplateBuilder}.
* The {@code retryPolicy} of the returned {@link RetryTemplate} is always an instance
* of {@link CompositeRetryPolicy}, that consists of one base policy, and of
* {@link BinaryExceptionClassifierRetryPolicy}. The motivation is: whatever base
* policy we use, exception classification is extremely recommended.
*
* @return new instance of {@link RetryTemplate}
*/
public RetryTemplate build() {
RetryTemplate retryTemplate = new RetryTemplate();
// Exception classifier
BinaryExceptionClassifier exceptionClassifier = classifierBuilder != null ? classifierBuilder.build() : BinaryExceptionClassifier.defaultClassifier();
if (this.baseRetryPolicy == null) {
this.baseRetryPolicy = new MaxAttemptsRetryPolicy();
}
CompositeRetryPolicy finalPolicy = new CompositeRetryPolicy();
finalPolicy.setPolicies(new RetryPolicy[] { baseRetryPolicy, new BinaryExceptionClassifierRetryPolicy(exceptionClassifier) });
retryTemplate.setRetryPolicy(finalPolicy);
if (this.backOffPolicy == null) {
this.backOffPolicy = new NoBackOffPolicy();
}
retryTemplate.setBackOffPolicy(this.backOffPolicy);
if (this.listeners != null) {
retryTemplate.setListeners(this.listeners.toArray(new RetryListener[0]));
}
return retryTemplate;
}
use of cn.taketoday.retry.policy.MaxAttemptsRetryPolicy 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);
}
Aggregations