BATCH-804: add interface for RetryState

This commit is contained in:
dsyer
2008-09-02 09:24:37 +00:00
parent 91ecbc6382
commit 311779037a
13 changed files with 265 additions and 111 deletions

View File

@@ -24,7 +24,7 @@ import junit.framework.TestCase;
import org.springframework.batch.retry.RecoveryCallback;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.support.DefaultRetryState;
import org.springframework.batch.retry.support.RetryTemplate;
public class FatalExceptionRetryPolicyTests extends TestCase {
@@ -82,14 +82,14 @@ public class FatalExceptionRetryPolicyTests extends TestCase {
Object result = null;
try {
retryTemplate.execute(callback, recoveryCallback, new RetryState("foo"));
retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
// If stateful we have to always rethrow. Clients who want special
// cases have to implement them in the callback
}
result = retryTemplate.execute(callback, recoveryCallback, new RetryState("foo"));
result = retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
// Callback is called once: the recovery path should also be called
assertEquals(1, callback.attempts);
assertEquals("bar", result);

View File

@@ -24,9 +24,10 @@ import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.support.DefaultRetryState;
import org.springframework.batch.retry.support.RetryTemplate;
/**
@@ -39,7 +40,7 @@ public class StatefulRetryIntegrationTests {
public void testExternalRetryWithFailAndNoRetry() throws Exception {
MockRetryCallback callback = new MockRetryCallback();
RetryState retryState = new RetryState("foo");
RetryState retryState = new DefaultRetryState("foo");
RetryTemplate retryTemplate = new RetryTemplate();
MapRetryContextCache cache = new MapRetryContextCache();
@@ -80,7 +81,7 @@ public class StatefulRetryIntegrationTests {
public void testExternalRetryWithSuccessOnRetry() throws Exception {
MockRetryCallback callback = new MockRetryCallback();
RetryState retryState = new RetryState("foo");
RetryState retryState = new DefaultRetryState("foo");
RetryTemplate retryTemplate = new RetryTemplate();
MapRetryContextCache cache = new MapRetryContextCache();

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.retry.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.batch.support.Classifier;
/**
* @author Dave Syer
*
*/
public class DefaultRetryStateTests {
/**
* Test method for
* {@link org.springframework.batch.retry.support.DefaultRetryState#DefaultRetryState(java.lang.Object, boolean, org.springframework.batch.support.Classifier)}.
*/
@Test
public void testDefaultRetryStateObjectBooleanClassifierOfQsuperThrowableBoolean() {
DefaultRetryState state = new DefaultRetryState("foo", true, new Classifier<Throwable, Boolean>() {
public Boolean classify(Throwable classifiable) {
return false;
}
});
assertEquals("foo", state.getKey());
assertTrue(state.isForceRefresh());
assertFalse(state.rollbackFor(null));
}
/**
* Test method for
* {@link org.springframework.batch.retry.support.DefaultRetryState#DefaultRetryState(java.lang.Object, org.springframework.batch.support.Classifier)}.
*/
@Test
public void testDefaultRetryStateObjectClassifierOfQsuperThrowableBoolean() {
DefaultRetryState state = new DefaultRetryState("foo", new Classifier<Throwable, Boolean>() {
public Boolean classify(Throwable classifiable) {
return false;
}
});
assertEquals("foo", state.getKey());
assertFalse(state.isForceRefresh());
assertFalse(state.rollbackFor(null));
}
/**
* Test method for
* {@link org.springframework.batch.retry.support.DefaultRetryState#DefaultRetryState(java.lang.Object, boolean)}.
*/
@Test
public void testDefaultRetryStateObjectBoolean() {
DefaultRetryState state = new DefaultRetryState("foo", true);
assertEquals("foo", state.getKey());
assertTrue(state.isForceRefresh());
assertTrue(state.rollbackFor(null));
}
/**
* Test method for
* {@link org.springframework.batch.retry.support.DefaultRetryState#DefaultRetryState(java.lang.Object)}.
*/
@Test
public void testDefaultRetryStateObject() {
DefaultRetryState state = new DefaultRetryState("foo");
assertEquals("foo", state.getKey());
assertFalse(state.isForceRefresh());
assertTrue(state.rollbackFor(null));
}
}

View File

@@ -29,7 +29,6 @@ import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.RecoveryCallback;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.backoff.BackOffContext;
import org.springframework.batch.retry.backoff.BackOffInterruptedException;
import org.springframework.batch.retry.backoff.BackOffPolicy;
@@ -130,8 +129,7 @@ public class RetryTemplateTests {
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(attempts));
BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections
.<Class<? extends Throwable>> singleton(IllegalArgumentException.class), false);
retryTemplate.setRollbackClassifier(classifier);
retryTemplate.execute(callback, new RetryState("foo"));
retryTemplate.execute(callback, new DefaultRetryState("foo",classifier));
assertEquals(attempts, callback.attempts);
}

View File

@@ -31,12 +31,12 @@ import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.RecoveryCallback;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryException;
import org.springframework.batch.retry.RetryPolicy;
import org.springframework.batch.retry.RetryState;
import org.springframework.batch.retry.policy.MapRetryContextCache;
import org.springframework.batch.retry.policy.NeverRetryPolicy;
import org.springframework.batch.retry.policy.SimpleRetryPolicy;
@@ -53,7 +53,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testOpenSunnyDay() throws Exception {
RetryContext context = retryTemplate.open(new NeverRetryPolicy(), new RetryState("foo"));
RetryContext context = retryTemplate.open(new NeverRetryPolicy(), new DefaultRetryState("foo"));
assertNotNull(context);
// we haven't called the processor yet...
assertEquals(0, count);
@@ -62,7 +62,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testRegisterThrowable() {
NeverRetryPolicy retryPolicy = new NeverRetryPolicy();
RetryState state = new RetryState("foo");
RetryState state = new DefaultRetryState("foo");
RetryContext context = retryTemplate.open(retryPolicy, state);
assertNotNull(context);
retryTemplate.registerThrowable(retryPolicy, state, context, new Exception());
@@ -72,7 +72,7 @@ public class StatefulRecoveryRetryTests {
@Test
public void testClose() throws Exception {
NeverRetryPolicy retryPolicy = new NeverRetryPolicy();
RetryState state = new RetryState("foo");
RetryState state = new DefaultRetryState("foo");
RetryContext context = retryTemplate.open(retryPolicy, state);
assertNotNull(context);
retryTemplate.registerThrowable(retryPolicy, state, context, new Exception());
@@ -96,7 +96,7 @@ public class StatefulRecoveryRetryTests {
public void testRecover() throws Exception {
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
final String input = "foo";
RetryState state = new RetryState(input);
RetryState state = new DefaultRetryState(input);
RetryCallback<String> callback = new RetryCallback<String>() {
public String doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
@@ -132,9 +132,8 @@ public class StatefulRecoveryRetryTests {
.<Class<? extends Throwable>> singleton(DataAccessException.class));
// ...but not these:
assertFalse(classifier.classify(new RuntimeException()));
retryTemplate.setRollbackClassifier(classifier);
final String input = "foo";
RetryState state = new RetryState(input);
RetryState state = new DefaultRetryState(input,classifier);
RetryCallback<String> callback = new RetryCallback<String>() {
public String doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
@@ -161,7 +160,7 @@ public class StatefulRecoveryRetryTests {
retryTemplate.setRetryPolicy(retryPolicy);
final String input = "foo";
RetryState state = new RetryState(input);
RetryState state = new DefaultRetryState(input);
RetryCallback<String> callback = new RetryCallback<String>() {
public String doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
@@ -195,7 +194,7 @@ public class StatefulRecoveryRetryTests {
RetryPolicy retryPolicy = new SimpleRetryPolicy(3);
retryTemplate.setRetryPolicy(retryPolicy);
final StringHolder item = new StringHolder("bar");
RetryState state = new RetryState(item);
RetryState state = new DefaultRetryState(item);
RetryCallback<StringHolder> callback = new RetryCallback<StringHolder>() {
public StringHolder doWithRetry(RetryContext context) throws Exception {
@@ -247,7 +246,7 @@ public class StatefulRecoveryRetryTests {
};
try {
retryTemplate.execute(callback, new RetryState("foo"));
retryTemplate.execute(callback, new DefaultRetryState("foo"));
fail("Expected RuntimeException");
}
catch (RuntimeException e) {
@@ -255,7 +254,7 @@ public class StatefulRecoveryRetryTests {
}
try {
retryTemplate.execute(callback, new RetryState("bar"));
retryTemplate.execute(callback, new DefaultRetryState("bar"));
fail("Expected RetryException");
}
catch (RetryException e) {
@@ -271,7 +270,7 @@ public class StatefulRecoveryRetryTests {
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
final StringHolder item = new StringHolder("foo");
RetryState state = new RetryState(item);
RetryState state = new DefaultRetryState(item);
RetryCallback<Object> callback = new RetryCallback<Object>() {
public Object doWithRetry(RetryContext context) throws Exception {