diff --git a/src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java b/src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java index dd5bdba..62ee2e4 100644 --- a/src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java +++ b/src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java @@ -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 { *

* 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 { *

* 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. + *

+ * Warn: touching this method drops default {@code retryOn(Exception.class)} and you + * should configure whole classifier from scratch. + *

+ * 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> throwables) { + for (final Class throwable : throwables) { + classifierBuilder().retryOn(throwable); + } + return this; + } + + /** + * Add all throwables to the black list of retryable exceptions. + *

+ * Warn: touching this method drops default {@code retryOn(Exception.class)} and you + * should configure whole classifier from scratch. + *

+ * 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> throwables) { + for (final Class throwable : throwables) { + classifierBuilder().notRetryOn(throwable); + } + return this; + } + /** * Suppose throwing a {@code new MyLogicException(new IOException())}. This template * will not retry on it:

{@code
diff --git a/src/test/java/org/springframework/retry/support/RetryTemplateBuilderTest.java b/src/test/java/org/springframework/retry/support/RetryTemplateBuilderTest.java
index f158f03..7033432 100644
--- a/src/test/java/org/springframework/retry/support/RetryTemplateBuilderTest.java
+++ b/src/test/java/org/springframework/retry/support/RetryTemplateBuilderTest.java
@@ -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.>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.>singletonList(IOException.class))
+					 .notRetryOn(Collections.>singletonList(OutOfMemoryError.class));
+	}
+
 	/* ---------------- BackOff -------------- */
 
 	@Test(expected = IllegalArgumentException.class)