GH-56: Add Retry All Except... Option

Resolves: #56
This commit is contained in:
Gary Russell
2016-10-04 12:30:47 -04:00
committed by Dave Syer
parent 23f2a35958
commit 5a4f4c6a6a
2 changed files with 37 additions and 2 deletions

View File

@@ -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<Class<? extends Throwable>, 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<Class<? extends Throwable>, 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);
}

View File

@@ -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
.<Class<? extends Throwable>, 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();