GH-254: Add RetryTemplBuilder.(not)RetryOn(List)

Fixes https://github.com/spring-projects/spring-retry/issues/254

Add method with list parameter by overloading existed method
This commit is contained in:
Kim In Hoi
2022-01-19 01:09:43 +09:00
committed by GitHub
parent 2eb162a59d
commit f107a77cda
2 changed files with 58 additions and 3 deletions

View File

@@ -82,6 +82,7 @@ import org.springframework.util.Assert;
*
* @author Aleksandr Shamukov
* @author Artem Bilan
* @author Kim In Hoi
* @since 1.3
*/
public class RetryTemplateBuilder {
@@ -276,7 +277,7 @@ public class RetryTemplateBuilder {
* <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
@@ -295,7 +296,7 @@ public class RetryTemplateBuilder {
* <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
@@ -306,6 +307,50 @@ public class RetryTemplateBuilder {
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
* @since 1.3.2
* @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
* @since 1.3.2
* @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

View File

@@ -50,6 +50,7 @@ import static org.springframework.retry.util.test.TestUtils.getPropertyValue;
* follow project's style.
*
* @author Aleksandr Shamukov
* @author Kim In Hoi
*/
public class RetryTemplateBuilderTest {
@@ -77,13 +78,15 @@ public class RetryTemplateBuilderTest {
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()));
@@ -158,6 +161,13 @@ public class RetryTemplateBuilderTest {
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)