From 5a4f4c6a6a3f305d93364e23697a8a42fd3f0695 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 4 Oct 2016 12:30:47 -0400 Subject: [PATCH] GH-56: Add Retry All Except... Option Resolves: #56 --- .../retry/policy/SimpleRetryPolicy.java | 22 +++++++++++++++++-- .../retry/policy/SimpleRetryPolicyTests.java | 17 ++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java index f0421c1..5143959 100644 --- a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java +++ b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java @@ -80,14 +80,32 @@ public class SimpleRetryPolicy implements RetryPolicy { * a match is found. * * @param maxAttempts the maximum number of attempts - * @param retryableExceptions the map of exceptions that are retryable + * @param retryableExceptions the map of exceptions that are retryable based on the + * map value (true/false). * @param traverseCauses is this clause traversable */ public SimpleRetryPolicy(int maxAttempts, Map, Boolean> retryableExceptions, boolean traverseCauses) { + this(maxAttempts, retryableExceptions, traverseCauses, false); + } + + /** + * Create a {@link SimpleRetryPolicy} with the specified number of retry + * attempts. If traverseCauses is true, the exception causes will be traversed until + * a match is found. The default value indicates whether to retry or not for exceptions + * (or super classes) are not found in the map. + * + * @param maxAttempts the maximum number of attempts + * @param retryableExceptions the map of exceptions that are retryable based on the + * map value (true/false). + * @param traverseCauses is this clause traversable + * @param defaultValue the default action. + */ + public SimpleRetryPolicy(int maxAttempts, Map, Boolean> retryableExceptions, + boolean traverseCauses, boolean defaultValue) { super(); this.maxAttempts = maxAttempts; - this.retryableClassifier = new BinaryExceptionClassifier(retryableExceptions); + this.retryableClassifier = new BinaryExceptionClassifier(retryableExceptions, defaultValue); this.retryableClassifier.setTraverseCauses(traverseCauses); } diff --git a/src/test/java/org/springframework/retry/policy/SimpleRetryPolicyTests.java b/src/test/java/org/springframework/retry/policy/SimpleRetryPolicyTests.java index dc33e69..8b037b6 100644 --- a/src/test/java/org/springframework/retry/policy/SimpleRetryPolicyTests.java +++ b/src/test/java/org/springframework/retry/policy/SimpleRetryPolicyTests.java @@ -53,6 +53,23 @@ public class SimpleRetryPolicyTests { assertFalse(policy.canRetry(context)); } + @Test + public void testWithExceptionDefaultAlwaysRetry() throws Exception { + + // We retry any exceptions except... + SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections + ., Boolean> singletonMap(IllegalStateException.class, false), true, true); + RetryContext context = policy.open(null); + + // ...so we can't retry this one... + policy.registerThrowable(context, new IllegalStateException()); + assertFalse(policy.canRetry(context)); + + // ...and we can retry this one... + policy.registerThrowable(context, new IllegalArgumentException()); + assertTrue(policy.canRetry(context)); + } + @Test public void testRetryLimitInitialState() throws Exception { SimpleRetryPolicy policy = new SimpleRetryPolicy();