OPEN - issue BATCH-777: Parametrise RetryCallback and related interfaces
RetryCallback and RecoveryCallback done
This commit is contained in:
@@ -20,8 +20,8 @@ import junit.framework.TestCase;
|
||||
|
||||
public class ItemRecoveryHandlerTests extends TestCase {
|
||||
|
||||
ItemRecoverer recoverer = new ItemRecoverer() {
|
||||
public Object recover(Object data, Throwable cause) {
|
||||
ItemRecoverer<String, String> recoverer = new ItemRecoverer<String,String>() {
|
||||
public String recover(String data, Throwable cause) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.batch.item.ItemKeyGenerator;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
|
||||
public class StubItemKeyGeneratorRecoverer implements ItemRecoverer, ItemKeyGenerator {
|
||||
|
||||
/**
|
||||
* Do nothing and return null. Subclassses should override to implement
|
||||
* recovery behaviour.
|
||||
*
|
||||
* @see org.springframework.batch.item.ItemRecoverer#recover(java.lang.Object,
|
||||
* Throwable)
|
||||
*
|
||||
* @return null.
|
||||
*/
|
||||
public Object recover(Object item, Throwable cause) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the item (assume it is its own key).
|
||||
*
|
||||
* @see org.springframework.batch.item.ItemKeyGenerator#getKey(java.lang.Object)
|
||||
*/
|
||||
public Object getKey(Object item) {
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -170,10 +170,10 @@ public class StatefulRetryOperationsInterceptorTests extends TestCase {
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
interceptor.setRecoverer(new ItemRecoverer() {
|
||||
public Object recover(Object data, Throwable cause) {
|
||||
interceptor.setRecoverer(new ItemRecoverer<Object, Object[]>() {
|
||||
public Object recover(Object[] data, Throwable cause) {
|
||||
count++;
|
||||
return data;
|
||||
return null;
|
||||
}
|
||||
});
|
||||
service.service("foo");
|
||||
@@ -192,10 +192,10 @@ public class StatefulRetryOperationsInterceptorTests extends TestCase {
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
interceptor.setRecoverer(new ItemRecoverer() {
|
||||
public Object recover(Object data, Throwable cause) {
|
||||
interceptor.setRecoverer(new ItemRecoverer<Collection<String>, Object[]>() {
|
||||
public Collection<String> recover(Object[] data, Throwable cause) {
|
||||
count++;
|
||||
return Collections.singleton(data);
|
||||
return Collections.singleton((String)data[0]);
|
||||
}
|
||||
});
|
||||
Collection<String> result = transformer.transform("foo");
|
||||
|
||||
@@ -38,20 +38,20 @@ public class RetryListenerTests extends TestCase {
|
||||
|
||||
public void testOpenInterceptors() throws Exception {
|
||||
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
|
||||
public boolean open(RetryContext context, RetryCallback callback) {
|
||||
public <T> boolean open(RetryContext context, RetryCallback<T> callback) {
|
||||
count++;
|
||||
list.add("1:" + count);
|
||||
return true;
|
||||
}
|
||||
}, new RetryListenerSupport() {
|
||||
public boolean open(RetryContext context, RetryCallback callback) {
|
||||
public <T> boolean open(RetryContext context, RetryCallback<T> callback) {
|
||||
count++;
|
||||
list.add("2:" + count);
|
||||
return true;
|
||||
}
|
||||
} });
|
||||
template.execute(new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
template.execute(new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@@ -62,14 +62,14 @@ public class RetryListenerTests extends TestCase {
|
||||
|
||||
public void testOpenCanVetoRetry() throws Exception {
|
||||
template.registerListener(new RetryListenerSupport() {
|
||||
public boolean open(RetryContext context, RetryCallback callback) {
|
||||
public <T> boolean open(RetryContext context, RetryCallback<T> callback) {
|
||||
list.add("1");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
try {
|
||||
template.execute(new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
template.execute(new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
count++;
|
||||
return null;
|
||||
}
|
||||
@@ -86,18 +86,18 @@ public class RetryListenerTests extends TestCase {
|
||||
|
||||
public void testCloseInterceptors() throws Exception {
|
||||
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
|
||||
public void close(RetryContext context, RetryCallback callback, Throwable t) {
|
||||
public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable t) {
|
||||
count++;
|
||||
list.add("1:" + count);
|
||||
}
|
||||
}, new RetryListenerSupport() {
|
||||
public void close(RetryContext context, RetryCallback callback, Throwable t) {
|
||||
public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable t) {
|
||||
count++;
|
||||
list.add("2:" + count);
|
||||
}
|
||||
} });
|
||||
template.execute(new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
template.execute(new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@@ -110,17 +110,17 @@ public class RetryListenerTests extends TestCase {
|
||||
public void testOnError() throws Exception {
|
||||
template.setRetryPolicy(new NeverRetryPolicy());
|
||||
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
|
||||
public void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
|
||||
public <T> void onError(RetryContext context, RetryCallback<T> callback, Throwable throwable) {
|
||||
list.add("1");
|
||||
}
|
||||
}, new RetryListenerSupport() {
|
||||
public void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
|
||||
public <T> void onError(RetryContext context, RetryCallback<T> callback, Throwable throwable) {
|
||||
list.add("2");
|
||||
}
|
||||
} });
|
||||
try {
|
||||
template.execute(new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
template.execute(new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
count++;
|
||||
throw new IllegalStateException("foo");
|
||||
}
|
||||
@@ -140,14 +140,14 @@ public class RetryListenerTests extends TestCase {
|
||||
|
||||
public void testCloseInterceptorsAfterRetry() throws Exception {
|
||||
template.registerListener(new RetryListenerSupport() {
|
||||
public void close(RetryContext context, RetryCallback callback, Throwable t) {
|
||||
public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable t) {
|
||||
list.add("" + count);
|
||||
// The last attempt should have been successful:
|
||||
assertNull(t);
|
||||
}
|
||||
});
|
||||
template.execute(new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
template.execute(new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
if (count++ < 1)
|
||||
throw new RuntimeException("Retry!");
|
||||
return null;
|
||||
|
||||
@@ -44,8 +44,8 @@ public class FatalExceptionRetryPolicyTests extends TestCase {
|
||||
add(IllegalStateException.class);
|
||||
}
|
||||
});
|
||||
RecoveryCallback recoveryCallback = new RecoveryCallback() {
|
||||
public Object recover(RetryContext context) throws Exception {
|
||||
RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
|
||||
public String recover(RetryContext context) throws Exception {
|
||||
return "bar";
|
||||
}
|
||||
};
|
||||
@@ -77,8 +77,8 @@ public class FatalExceptionRetryPolicyTests extends TestCase {
|
||||
add(IllegalStateException.class);
|
||||
}
|
||||
});
|
||||
RecoveryCallback recoveryCallback = new RecoveryCallback() {
|
||||
public Object recover(RetryContext context) throws Exception {
|
||||
RecoveryCallback<String>recoveryCallback = new RecoveryCallback<String>() {
|
||||
public String recover(RetryContext context) throws Exception {
|
||||
return "bar";
|
||||
}
|
||||
};
|
||||
@@ -98,15 +98,15 @@ public class FatalExceptionRetryPolicyTests extends TestCase {
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
|
||||
private static class MockRetryCallback implements RetryCallback {
|
||||
private static class MockRetryCallback implements RetryCallback<String> {
|
||||
|
||||
private int attempts;
|
||||
|
||||
private Exception exceptionToThrow = new Exception();
|
||||
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
this.attempts++;
|
||||
// Otherwise just barf...
|
||||
// Just barf...
|
||||
throw this.exceptionToThrow;
|
||||
}
|
||||
|
||||
|
||||
@@ -113,10 +113,10 @@ public class StatefulRetryIntegrationTests {
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private final class MockRetryCallback implements RetryCallback {
|
||||
private final class MockRetryCallback implements RetryCallback<String> {
|
||||
int attempts = 0;
|
||||
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
attempts++;
|
||||
if (attempts < 2) {
|
||||
throw new RuntimeException();
|
||||
|
||||
@@ -21,8 +21,6 @@ import junit.framework.TestCase;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.context.RetryContextSupport;
|
||||
import org.springframework.batch.retry.support.RetrySynchronizationManager;
|
||||
import org.springframework.batch.retry.support.RetryTemplate;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -43,7 +41,7 @@ public class RetrySynchronizationManagerTests extends TestCase {
|
||||
RetryContext status = RetrySynchronizationManager.getContext();
|
||||
assertNull(status);
|
||||
|
||||
template.execute(new RetryCallback() {
|
||||
template.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext status) throws Exception {
|
||||
RetryContext global = RetrySynchronizationManager.getContext();
|
||||
assertNotNull(status);
|
||||
|
||||
@@ -137,7 +137,7 @@ public class RetryTemplateTests extends TestCase {
|
||||
public void testEarlyTermination() throws Exception {
|
||||
try {
|
||||
RetryTemplate retryTemplate = new RetryTemplate();
|
||||
retryTemplate.execute(new RetryCallback() {
|
||||
retryTemplate.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext status) throws Exception {
|
||||
status.setExhaustedOnly();
|
||||
throw new IllegalStateException("Retry this operation");
|
||||
@@ -155,11 +155,11 @@ public class RetryTemplateTests extends TestCase {
|
||||
public void testNestedContexts() throws Exception {
|
||||
RetryTemplate outer = new RetryTemplate();
|
||||
final RetryTemplate inner = new RetryTemplate();
|
||||
outer.execute(new RetryCallback() {
|
||||
outer.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext status) throws Exception {
|
||||
context = status;
|
||||
count++;
|
||||
Object result = inner.execute(new RetryCallback() {
|
||||
Object result = inner.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext status) throws Exception {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
@@ -180,7 +180,7 @@ public class RetryTemplateTests extends TestCase {
|
||||
RetryTemplate retryTemplate = new RetryTemplate();
|
||||
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
|
||||
try {
|
||||
retryTemplate.execute(new RetryCallback() {
|
||||
retryTemplate.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
throw new Error("Realllly bad!");
|
||||
}
|
||||
@@ -200,7 +200,7 @@ public class RetryTemplateTests extends TestCase {
|
||||
}
|
||||
});
|
||||
try {
|
||||
retryTemplate.execute(new RetryCallback() {
|
||||
retryTemplate.execute(new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
throw new RuntimeException("Bad!");
|
||||
}
|
||||
@@ -212,7 +212,7 @@ public class RetryTemplateTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockRetryCallback implements RetryCallback {
|
||||
private static class MockRetryCallback implements RetryCallback<Object> {
|
||||
|
||||
private int attempts;
|
||||
|
||||
|
||||
@@ -94,13 +94,13 @@ public class StatefulRecoveryRetryTests {
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
|
||||
final String input = "foo";
|
||||
RetryState state = new RetryState(input);
|
||||
RetryCallback callback = new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
RetryCallback<String> callback = new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
throw new RuntimeException("Barf!");
|
||||
}
|
||||
};
|
||||
RecoveryCallback recoveryCallback = new RecoveryCallback() {
|
||||
public Object recover(RetryContext context) {
|
||||
RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
|
||||
public String recover(RetryContext context) {
|
||||
count++;
|
||||
list.add(input);
|
||||
return input;
|
||||
@@ -128,8 +128,8 @@ public class StatefulRecoveryRetryTests {
|
||||
|
||||
final String input = "foo";
|
||||
RetryState state = new RetryState(input);
|
||||
RetryCallback callback = new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
RetryCallback<String> callback = new RetryCallback<String>() {
|
||||
public String doWithRetry(RetryContext context) throws Exception {
|
||||
throw new RuntimeException("Barf!");
|
||||
}
|
||||
};
|
||||
@@ -163,8 +163,8 @@ public class StatefulRecoveryRetryTests {
|
||||
final StringHolder item = new StringHolder("bar");
|
||||
RetryState state = new RetryState(item);
|
||||
|
||||
RetryCallback callback = new RetryCallback() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
RetryCallback<StringHolder> callback = new RetryCallback<StringHolder>() {
|
||||
public StringHolder doWithRetry(RetryContext context) throws Exception {
|
||||
// This simulates what happens if someone uses a primary key
|
||||
// for hashCode and equals and then relies on default key
|
||||
// generator
|
||||
@@ -195,7 +195,7 @@ public class StatefulRecoveryRetryTests {
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
|
||||
retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
|
||||
|
||||
RetryCallback callback = new RetryCallback() {
|
||||
RetryCallback<Object> callback = new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
count++;
|
||||
throw new RuntimeException("Barf!");
|
||||
@@ -229,13 +229,13 @@ public class StatefulRecoveryRetryTests {
|
||||
final StringHolder item = new StringHolder("foo");
|
||||
RetryState state = new RetryState(item);
|
||||
|
||||
RetryCallback callback = new RetryCallback() {
|
||||
RetryCallback<Object> callback = new RetryCallback<Object>() {
|
||||
public Object doWithRetry(RetryContext context) throws Exception {
|
||||
count++;
|
||||
throw new RuntimeException("Barf!");
|
||||
}
|
||||
};
|
||||
RecoveryCallback recoveryCallback = new RecoveryCallback() {
|
||||
RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {
|
||||
public Object recover(RetryContext context) throws Exception {
|
||||
return null;
|
||||
}
|
||||
@@ -260,36 +260,18 @@ public class StatefulRecoveryRetryTests {
|
||||
|
||||
private String string;
|
||||
|
||||
/**
|
||||
* @param string
|
||||
*/
|
||||
public StringHolder(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
return string.equals(((StringHolder) obj).string);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
return string.hashCode();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
public String toString() {
|
||||
return "String: " + string + " (hash = " + hashCode() + ")";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user