Skip to content

Commit f107a77

Browse files
authored
GH-254: Add RetryTemplBuilder.(not)RetryOn(List)
Fixes #254 Add method with list parameter by overloading existed method
1 parent 2eb162a commit f107a77

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/main/java/org/springframework/retry/support/RetryTemplateBuilder.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
*
8383
* @author Aleksandr Shamukov
8484
* @author Artem Bilan
85+
* @author Kim In Hoi
8586
* @since 1.3
8687
*/
8788
public class RetryTemplateBuilder {
@@ -276,7 +277,7 @@ public RetryTemplateBuilder customBackoff(BackOffPolicy backOffPolicy) {
276277
* <p>
277278
* You should select the way you want to configure exception classifier: white list or
278279
* black list. If you choose white list - use this method, if black - use
279-
* {@link #notRetryOn(Class)}
280+
* {@link #notRetryOn(Class)} or {@link #notRetryOn(List)}
280281
* @param throwable to be retryable (with it's subclasses)
281282
* @return this
282283
* @see BinaryExceptionClassifierBuilder#retryOn
@@ -295,7 +296,7 @@ public RetryTemplateBuilder retryOn(Class<? extends Throwable> throwable) {
295296
* <p>
296297
* You should select the way you want to configure exception classifier: white list or
297298
* black list. If you choose black list - use this method, if white - use
298-
* {@link #retryOn(Class)}
299+
* {@link #retryOn(Class)} or {@link #retryOn(List)}
299300
* @param throwable to be not retryable (with it's subclasses)
300301
* @return this
301302
* @see BinaryExceptionClassifierBuilder#notRetryOn
@@ -306,6 +307,50 @@ public RetryTemplateBuilder notRetryOn(Class<? extends Throwable> throwable) {
306307
return this;
307308
}
308309

310+
/**
311+
* Add all throwables to the while list of retryable exceptions.
312+
* <p>
313+
* Warn: touching this method drops default {@code retryOn(Exception.class)} and you
314+
* should configure whole classifier from scratch.
315+
* <p>
316+
* You should select the way you want to configure exception classifier: white list or
317+
* black list. If you choose white list - use this method, if black - use
318+
* {@link #notRetryOn(Class)} or {@link #notRetryOn(List)}
319+
* @param throwables to be retryable (with it's subclasses)
320+
* @return this
321+
* @since 1.3.2
322+
* @see BinaryExceptionClassifierBuilder#retryOn
323+
* @see BinaryExceptionClassifier
324+
*/
325+
public RetryTemplateBuilder retryOn(List<Class<? extends Throwable>> throwables) {
326+
for (final Class<? extends Throwable> throwable : throwables) {
327+
classifierBuilder().retryOn(throwable);
328+
}
329+
return this;
330+
}
331+
332+
/**
333+
* Add all throwables to the black list of retryable exceptions.
334+
* <p>
335+
* Warn: touching this method drops default {@code retryOn(Exception.class)} and you
336+
* should configure whole classifier from scratch.
337+
* <p>
338+
* You should select the way you want to configure exception classifier: white list or
339+
* black list. If you choose black list - use this method, if white - use
340+
* {@link #retryOn(Class)} or {@link #retryOn(List)}
341+
* @param throwables to be not retryable (with it's subclasses)
342+
* @return this
343+
* @since 1.3.2
344+
* @see BinaryExceptionClassifierBuilder#notRetryOn
345+
* @see BinaryExceptionClassifier
346+
*/
347+
public RetryTemplateBuilder notRetryOn(List<Class<? extends Throwable>> throwables) {
348+
for (final Class<? extends Throwable> throwable : throwables) {
349+
classifierBuilder().notRetryOn(throwable);
350+
}
351+
return this;
352+
}
353+
309354
/**
310355
* Suppose throwing a {@code new MyLogicException(new IOException())}. This template
311356
* will not retry on it: <pre>{@code

src/test/java/org/springframework/retry/support/RetryTemplateBuilderTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* follow project's style.
5151
*
5252
* @author Aleksandr Shamukov
53+
* @author Kim In Hoi
5354
*/
5455
public class RetryTemplateBuilderTest {
5556

@@ -77,13 +78,15 @@ public void testBasicCustomization() {
7778
RetryListener listener2 = mock(RetryListener.class);
7879

7980
RetryTemplate template = RetryTemplate.builder().maxAttempts(10).exponentialBackoff(99, 1.5, 1717)
80-
.retryOn(IOException.class).traversingCauses().withListener(listener1)
81+
.retryOn(IOException.class).retryOn(Collections.<Class<? extends Throwable>>singletonList(IllegalArgumentException.class))
82+
.traversingCauses().withListener(listener1)
8183
.withListeners(Collections.singletonList(listener2)).build();
8284

8385
PolicyTuple policyTuple = PolicyTuple.extractWithAsserts(template);
8486

8587
BinaryExceptionClassifier classifier = policyTuple.exceptionClassifierRetryPolicy.getExceptionClassifier();
8688
Assert.assertTrue(classifier.classify(new FileNotFoundException()));
89+
Assert.assertTrue(classifier.classify(new IllegalArgumentException()));
8790
Assert.assertFalse(classifier.classify(new RuntimeException()));
8891
Assert.assertFalse(classifier.classify(new OutOfMemoryError()));
8992

@@ -158,6 +161,13 @@ public void testFailOnNotationMix() {
158161
RetryTemplate.builder().retryOn(IOException.class).notRetryOn(OutOfMemoryError.class);
159162
}
160163

164+
@Test(expected = IllegalArgumentException.class)
165+
public void testFailOnNotationsMix() {
166+
RetryTemplate.builder()
167+
.retryOn(Collections.<Class<? extends Throwable>>singletonList(IOException.class))
168+
.notRetryOn(Collections.<Class<? extends Throwable>>singletonList(OutOfMemoryError.class));
169+
}
170+
161171
/* ---------------- BackOff -------------- */
162172

163173
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)