Add convenience constructor to SimpleRetryPolicy

This commit is contained in:
Dave Syer
2016-09-26 10:04:08 +01:00
parent 48935e745e
commit 18705bd905
8 changed files with 37 additions and 80 deletions

View File

@@ -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
.<Class<? extends Throwable>, 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
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));
}
/**
* Create a {@link SimpleRetryPolicy} with the specified number of retry
* attempts.

View File

@@ -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
.<Class<? extends Throwable>, Boolean>singletonMap(Exception.class, true));
private volatile RetryPolicy retryPolicy = new SimpleRetryPolicy(3);
private volatile RetryListener[] listeners = new RetryListener[0];

View File

@@ -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.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
template.setRetryPolicy(new SimpleRetryPolicy(1));
interceptor.setRetryOperations(template);
interceptor.setRecoverer(new MethodInvocationRecoverer<Void>() {
public Void recover(Object[] args, Throwable cause) {
@@ -121,9 +118,7 @@ public class RetryOperationsInterceptorTests {
}
});
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new SimpleRetryPolicy(2,
Collections.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
template.setRetryPolicy(new SimpleRetryPolicy(2));
interceptor.setRetryOperations(template);
service.service();
assertEquals(2, count);

View File

@@ -151,9 +151,7 @@ public class StatefulRetryOperationsInterceptorTests {
}
});
interceptor.setRetryOperations(retryTemplate);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2,
Collections.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
try {
transformer.transform("foo");
fail("Expected Exception.");

View File

@@ -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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
try {
retryTemplate.execute(callback, retryState);

View File

@@ -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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
retryTemplate.execute(callback, new RecoveryCallback<Object>() {
@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.<Class<? extends Throwable>, Boolean>singletonMap(
Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x));
for (int i = 0; i < x+1; i++) {
try {
retryTemplate.execute(callback, new RecoveryCallback<Object>() {

View File

@@ -59,9 +59,7 @@ public class RetryTemplateTests {
MockRetryCallback callback = new MockRetryCallback();
callback.setAttemptsBeforeSuccess(x);
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(x, Collections
.<Class<? extends Throwable>, 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
.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, Boolean> singletonMap(
Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
final Object value = new Object();
Object result = retryTemplate.execute(callback, new RecoveryCallback<Object>() {
@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.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, 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
.<Class<? extends Throwable>, 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.<Class<? extends Throwable>, Boolean> singletonMap(
Exception.class, true)));
tested.setRetryPolicy(new SimpleRetryPolicy(1));
BackOffPolicy bop = createStrictMock(BackOffPolicy.class);
BackOffContext backOffContext = new BackOffContext() {

View File

@@ -82,8 +82,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testRecover() throws Throwable {
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
final String input = "foo";
RetryState state = new DefaultRetryState(input);
RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {
@@ -115,8 +114,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
// Roll back for these:
BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections
.<Class<? extends Throwable>> singleton(DataAccessException.class));
@@ -146,8 +144,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
RetryPolicy retryPolicy = new SimpleRetryPolicy(1, Collections
.<Class<? extends Throwable>, 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
.<Class<? extends Throwable>, 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
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
RetryCallback<Object, Exception> callback = new RetryCallback<Object, Exception>() {
@@ -259,8 +254,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testCacheCapacityNotReachedIfRecovered() throws Throwable {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1, Collections
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1);
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
final StringHolder item = new StringHolder("foo");