Skip to content

Commit

Permalink
spring-projectsGH-254: Nice to have: notRetryOn() and retryOn() in Re…
Browse files Browse the repository at this point in the history
…tryTemplateBuilder

Fixes spring-projects#254

Add method with list parameter by overloading existed method
  • Loading branch information
hotire committed Jan 13, 2022
1 parent 6c59c0d commit b9dffcf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public RetryTemplateBuilder customBackoff(BackOffPolicy backOffPolicy) {
* <p>
* You should select the way you want to configure exception classifier: white list or
* black list. If you choose white list - use this method, if black - use
* {@link #notRetryOn(Class)}
* {@link #notRetryOn(Class)} or {@link #notRetryOn(List)}
* @param throwable to be retryable (with it's subclasses)
* @return this
* @see BinaryExceptionClassifierBuilder#retryOn
Expand All @@ -295,7 +295,7 @@ public RetryTemplateBuilder retryOn(Class<? extends Throwable> throwable) {
* <p>
* You should select the way you want to configure exception classifier: white list or
* black list. If you choose black list - use this method, if white - use
* {@link #retryOn(Class)}
* {@link #retryOn(Class)} or {@link #retryOn(List)}
* @param throwable to be not retryable (with it's subclasses)
* @return this
* @see BinaryExceptionClassifierBuilder#notRetryOn
Expand All @@ -306,6 +306,48 @@ public RetryTemplateBuilder notRetryOn(Class<? extends Throwable> throwable) {
return this;
}

/**
* Add all throwables to the while list of retryable exceptions.
* <p>
* Warn: touching this method drops default {@code retryOn(Exception.class)} and you
* should configure whole classifier from scratch.
* <p>
* You should select the way you want to configure exception classifier: white list or
* black list. If you choose white list - use this method, if black - use
* {@link #notRetryOn(Class)} or {@link #notRetryOn(List)}
* @param throwables to be retryable (with it's subclasses)
* @return this
* @see BinaryExceptionClassifierBuilder#retryOn
* @see BinaryExceptionClassifier
*/
public RetryTemplateBuilder retryOn(List<Class<? extends Throwable>> throwables) {
for (final Class<? extends Throwable> throwable : throwables) {
classifierBuilder().retryOn(throwable);
}
return this;
}

/**
* Add all throwables to the black list of retryable exceptions.
* <p>
* Warn: touching this method drops default {@code retryOn(Exception.class)} and you
* should configure whole classifier from scratch.
* <p>
* You should select the way you want to configure exception classifier: white list or
* black list. If you choose black list - use this method, if white - use
* {@link #retryOn(Class)} or {@link #retryOn(List)}
* @param throwables to be not retryable (with it's subclasses)
* @return this
* @see BinaryExceptionClassifierBuilder#notRetryOn
* @see BinaryExceptionClassifier
*/
public RetryTemplateBuilder notRetryOn(List<Class<? extends Throwable>> throwables) {
for (final Class<? extends Throwable> throwable : throwables) {
classifierBuilder().notRetryOn(throwable);
}
return this;
}

/**
* Suppose throwing a {@code new MyLogicException(new IOException())}. This template
* will not retry on it: <pre>{@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ public void testBasicCustomization() {
RetryListener listener2 = mock(RetryListener.class);

RetryTemplate template = RetryTemplate.builder().maxAttempts(10).exponentialBackoff(99, 1.5, 1717)
.retryOn(IOException.class).traversingCauses().withListener(listener1)
.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()));

Expand Down Expand Up @@ -158,6 +160,11 @@ public void testFailOnNotationMix() {
RetryTemplate.builder().retryOn(IOException.class).notRetryOn(OutOfMemoryError.class);
}

@Test(expected = IllegalArgumentException.class)
public void testFailOnNotationsMix() {
RetryTemplate.builder().retryOn(Collections.<Class<? extends Throwable>>singletonList(IOException.class)).notRetryOn(Collections.<Class<? extends Throwable>>singletonList(OutOfMemoryError.class));
}

/* ---------------- BackOff -------------- */

@Test(expected = IllegalArgumentException.class)
Expand Down

0 comments on commit b9dffcf

Please sign in to comment.