diff --git a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java index f0421c1..828e2df 100644 --- a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java +++ b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java @@ -56,13 +56,22 @@ public class SimpleRetryPolicy implements RetryPolicy { /** * Create a {@link SimpleRetryPolicy} with the default number of retry - * attempts. + * attempts, retrying all exceptions. */ public SimpleRetryPolicy() { this(DEFAULT_MAX_ATTEMPTS, Collections ., Boolean> singletonMap(Exception.class, true)); } + /** + * Create a {@link SimpleRetryPolicy} with the specified number of retry + * attempts, retrying all exceptions. + */ + public SimpleRetryPolicy(int maxAttempts) { + this(maxAttempts, Collections + ., Boolean> singletonMap(Exception.class, true)); + } + /** * Create a {@link SimpleRetryPolicy} with the specified number of retry * attempts. diff --git a/src/main/java/org/springframework/retry/support/RetryTemplate.java b/src/main/java/org/springframework/retry/support/RetryTemplate.java index 434b67c..416c631 100644 --- a/src/main/java/org/springframework/retry/support/RetryTemplate.java +++ b/src/main/java/org/springframework/retry/support/RetryTemplate.java @@ -18,7 +18,6 @@ package org.springframework.retry.support; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.apache.commons.logging.Log; @@ -80,8 +79,7 @@ public class RetryTemplate implements RetryOperations { private volatile BackOffPolicy backOffPolicy = new NoBackOffPolicy(); - private volatile RetryPolicy retryPolicy = new SimpleRetryPolicy(3, Collections - ., Boolean>singletonMap(Exception.class, true)); + private volatile RetryPolicy retryPolicy = new SimpleRetryPolicy(3); private volatile RetryListener[] listeners = new RetryListener[0]; diff --git a/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java b/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java index 57a33d5..139cdc5 100644 --- a/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java +++ b/src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java @@ -24,7 +24,6 @@ import static org.junit.Assert.fail; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.aopalliance.intercept.MethodInterceptor; @@ -96,9 +95,7 @@ public class RetryOperationsInterceptorTests { @Test public void testDefaultInterceptorWithRecovery() throws Exception { RetryTemplate template = new RetryTemplate(); - template.setRetryPolicy(new SimpleRetryPolicy(1, - Collections., Boolean>singletonMap( - Exception.class, true))); + template.setRetryPolicy(new SimpleRetryPolicy(1)); interceptor.setRetryOperations(template); interceptor.setRecoverer(new MethodInvocationRecoverer() { public Void recover(Object[] args, Throwable cause) { @@ -121,9 +118,7 @@ public class RetryOperationsInterceptorTests { } }); RetryTemplate template = new RetryTemplate(); - template.setRetryPolicy(new SimpleRetryPolicy(2, - Collections., Boolean>singletonMap( - Exception.class, true))); + template.setRetryPolicy(new SimpleRetryPolicy(2)); interceptor.setRetryOperations(template); service.service(); assertEquals(2, count); diff --git a/src/test/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptorTests.java b/src/test/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptorTests.java index 3f55764..617fd73 100644 --- a/src/test/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptorTests.java +++ b/src/test/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptorTests.java @@ -151,9 +151,7 @@ public class StatefulRetryOperationsInterceptorTests { } }); interceptor.setRetryOperations(retryTemplate); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2)); try { service.service("foo"); fail("Expected Exception."); @@ -173,9 +171,7 @@ public class StatefulRetryOperationsInterceptorTests { public void testTransformerWithSuccessfulRetry() throws Exception { ((Advised) transformer).addAdvice(interceptor); interceptor.setRetryOperations(retryTemplate); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2)); try { transformer.transform("foo"); fail("Expected Exception."); diff --git a/src/test/java/org/springframework/retry/policy/StatefulRetryIntegrationTests.java b/src/test/java/org/springframework/retry/policy/StatefulRetryIntegrationTests.java index 197c9ac..04086d7 100644 --- a/src/test/java/org/springframework/retry/policy/StatefulRetryIntegrationTests.java +++ b/src/test/java/org/springframework/retry/policy/StatefulRetryIntegrationTests.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.junit.Test; @@ -52,9 +51,7 @@ public class StatefulRetryIntegrationTests { RetryTemplate retryTemplate = new RetryTemplate(); MapRetryContextCache cache = new MapRetryContextCache(); retryTemplate.setRetryContextCache(cache); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1)); assertFalse(cache.containsKey("foo")); @@ -95,9 +92,7 @@ public class StatefulRetryIntegrationTests { RetryTemplate retryTemplate = new RetryTemplate(); MapRetryContextCache cache = new MapRetryContextCache(); retryTemplate.setRetryContextCache(cache); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2)); assertFalse(cache.containsKey("foo")); @@ -161,9 +156,7 @@ public class StatefulRetryIntegrationTests { RetryTemplate retryTemplate = new RetryTemplate(); MapRetryContextCache cache = new MapRetryContextCache(); retryTemplate.setRetryContextCache(cache); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1)); try { retryTemplate.execute(callback, retryState); diff --git a/src/test/java/org/springframework/retry/stats/StatisticsListenerTests.java b/src/test/java/org/springframework/retry/stats/StatisticsListenerTests.java index a080ace..f442f1a 100644 --- a/src/test/java/org/springframework/retry/stats/StatisticsListenerTests.java +++ b/src/test/java/org/springframework/retry/stats/StatisticsListenerTests.java @@ -19,8 +19,6 @@ package org.springframework.retry.stats; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.util.Collections; - import org.junit.Test; import org.springframework.retry.RecoveryCallback; import org.springframework.retry.RetryCallback; @@ -48,9 +46,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); retryTemplate.execute(callback); assertEquals(x, callback.attempts); RetryStatistics stats = repository.findOne("test"); @@ -70,9 +66,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); for (int i = 0; i < x; i++) { try { retryTemplate.execute(callback, state); @@ -98,9 +92,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x + 1); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); try { retryTemplate.execute(callback); } @@ -124,9 +116,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x + 1); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); for (int i = 0; i < x+1; i++) { try { retryTemplate.execute(callback, state); @@ -152,9 +142,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x + 1); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); retryTemplate.execute(callback, new RecoveryCallback() { @Override public Object recover(RetryContext context) throws Exception { @@ -179,9 +167,7 @@ public class StatisticsListenerTests { for (int x = 1; x <= 10; x++) { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x + 1); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, - Collections., Boolean>singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); for (int i = 0; i < x+1; i++) { try { retryTemplate.execute(callback, new RecoveryCallback() { diff --git a/src/test/java/org/springframework/retry/support/RetryTemplateTests.java b/src/test/java/org/springframework/retry/support/RetryTemplateTests.java index 3faf4bf..9dc94f9 100644 --- a/src/test/java/org/springframework/retry/support/RetryTemplateTests.java +++ b/src/test/java/org/springframework/retry/support/RetryTemplateTests.java @@ -59,9 +59,7 @@ public class RetryTemplateTests { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(x); RetryTemplate retryTemplate = new RetryTemplate(); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, Collections - ., Boolean> singletonMap(Exception.class, - true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); retryTemplate.execute(callback); assertEquals(x, callback.attempts); } @@ -86,9 +84,7 @@ public class RetryTemplateTests { } }; RetryTemplate retryTemplate = new RetryTemplate(); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, Collections - ., Boolean> singletonMap(Exception.class, - true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x)); retryTemplate.execute(callback); assertEquals(x, attempts.get()); } @@ -99,9 +95,7 @@ public class RetryTemplateTests { MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(3); RetryTemplate retryTemplate = new RetryTemplate(); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, - Collections., Boolean> singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2)); final Object value = new Object(); Object result = retryTemplate.execute(callback, new RecoveryCallback() { @Override @@ -130,9 +124,7 @@ public class RetryTemplateTests { callback.setAttemptsBeforeSuccess(Integer.MAX_VALUE); RetryTemplate retryTemplate = new RetryTemplate(); int retryAttempts = 2; - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(retryAttempts, - Collections., Boolean> singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(retryAttempts)); try { retryTemplate.execute(callback); fail("Expected IllegalArgumentException"); @@ -152,9 +144,7 @@ public class RetryTemplateTests { callback.setExceptionToThrow(new IllegalArgumentException()); RetryTemplate retryTemplate = new RetryTemplate(); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(attempts, - Collections., Boolean> singletonMap( - Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(attempts)); retryTemplate.execute(callback); assertEquals(attempts, callback.attempts); } @@ -210,10 +200,8 @@ public class RetryTemplateTests { MockBackOffStrategy backOff = new MockBackOffStrategy(); callback.setAttemptsBeforeSuccess(x); RetryTemplate retryTemplate = new RetryTemplate(); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(10)); retryTemplate.setBackOffPolicy(backOff); - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, Collections - ., Boolean> singletonMap(Exception.class, - true))); retryTemplate.execute(callback); assertEquals(x, callback.attempts); assertEquals(1, backOff.startCalls); @@ -357,9 +345,7 @@ public class RetryTemplateTests { public void testNoBackOffForRethrownException() throws Throwable { RetryTemplate tested = new RetryTemplate(); - tested.setRetryPolicy(new SimpleRetryPolicy(1, - Collections., Boolean> singletonMap( - Exception.class, true))); + tested.setRetryPolicy(new SimpleRetryPolicy(1)); BackOffPolicy bop = createStrictMock(BackOffPolicy.class); BackOffContext backOffContext = new BackOffContext() { diff --git a/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java b/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java index 3b69616..f737287 100644 --- a/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java +++ b/src/test/java/org/springframework/retry/support/StatefulRecoveryRetryTests.java @@ -82,8 +82,7 @@ public class StatefulRecoveryRetryTests { @Test public void testRecover() throws Throwable { - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections - ., Boolean> singletonMap(Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1)); final String input = "foo"; RetryState state = new DefaultRetryState(input); RetryCallback callback = new RetryCallback() { @@ -115,8 +114,7 @@ public class StatefulRecoveryRetryTests { @Test public void testSwitchToStatelessForNoRollback() throws Throwable { - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections - ., Boolean> singletonMap(Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1)); // Roll back for these: BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections .> singleton(DataAccessException.class)); @@ -146,8 +144,7 @@ public class StatefulRecoveryRetryTests { @Test public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable { - RetryPolicy retryPolicy = new SimpleRetryPolicy(1, Collections - ., Boolean> singletonMap(Exception.class, true)); + RetryPolicy retryPolicy = new SimpleRetryPolicy(1); retryTemplate.setRetryPolicy(retryPolicy); final String input = "foo"; @@ -182,8 +179,7 @@ public class StatefulRecoveryRetryTests { @Test public void testKeyGeneratorNotConsistentAfterFailure() throws Throwable { - RetryPolicy retryPolicy = new SimpleRetryPolicy(3, Collections - ., Boolean> singletonMap(Exception.class, true)); + RetryPolicy retryPolicy = new SimpleRetryPolicy(3); retryTemplate.setRetryPolicy(retryPolicy); final StringHolder item = new StringHolder("bar"); RetryState state = new DefaultRetryState(item); @@ -227,8 +223,7 @@ public class StatefulRecoveryRetryTests { @Test public void testCacheCapacity() throws Throwable { - retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections - ., Boolean> singletonMap(Exception.class, true))); + retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1)); retryTemplate.setRetryContextCache(new MapRetryContextCache(1)); RetryCallback callback = new RetryCallback() { @@ -259,8 +254,7 @@ public class StatefulRecoveryRetryTests { @Test public void testCacheCapacityNotReachedIfRecovered() throws Throwable { - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1, Collections - ., Boolean> singletonMap(Exception.class, true)); + SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1); retryTemplate.setRetryPolicy(retryPolicy); retryTemplate.setRetryContextCache(new MapRetryContextCache(2)); final StringHolder item = new StringHolder("foo");