IN PROGRESS - issue BATCH-491: checked exception handling
http://jira.springframework.org/browse/BATCH-491 exception handler signature changed to 'throws Throwable' so that it can simply rethrow without wrapping.
This commit is contained in:
@@ -52,7 +52,7 @@ public class SimpleRetryExceptionHandler extends RetryListenerSupport implements
|
||||
* @see org.springframework.batch.repeat.exception.handler.ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
|
||||
* java.lang.Throwable)
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
|
||||
// Only bother to check the delegate exception handler if we know that
|
||||
// retry is exhausted
|
||||
if (context.hasAttribute(EXHAUSTED)) {
|
||||
|
||||
@@ -54,7 +54,7 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
|
||||
*/
|
||||
public void testRethrowWhenRetryExhausted() {
|
||||
public void testRethrowWhenRetryExhausted() throws Throwable {
|
||||
|
||||
RetryPolicy retryPolicy = new NeverRetryPolicy();
|
||||
RuntimeException ex = new RuntimeException("foo");
|
||||
@@ -79,7 +79,7 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
|
||||
*/
|
||||
public void testNoRethrowWhenRetryNotExhausted() {
|
||||
public void testNoRethrowWhenRetryNotExhausted() throws Throwable {
|
||||
|
||||
RetryPolicy retryPolicy = new AlwaysRetryPolicy();
|
||||
RuntimeException ex = new RuntimeException("foo");
|
||||
|
||||
@@ -39,7 +39,7 @@ public class CompositeExceptionHandler implements ExceptionHandler {
|
||||
*
|
||||
* @see ExceptionHandler#handleException(RepeatContext, Throwable)
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
|
||||
for (int i = 0; i < handlers.length; i++) {
|
||||
ExceptionHandler handler = handlers[i];
|
||||
handler.handleException(context, throwable);
|
||||
|
||||
@@ -34,7 +34,7 @@ public class DefaultExceptionHandler implements ExceptionHandler {
|
||||
* @see org.springframework.batch.repeat.exception.ExceptionHandler#handleException(RepeatContext,
|
||||
* Throwable)
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
|
||||
rethrow(throwable);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,34 +20,29 @@ import org.springframework.batch.repeat.CompletionPolicy;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
|
||||
/**
|
||||
* Handler to allow strategies for rethrowing exceptions. Normally a
|
||||
* Handler to allow strategies for re-throwing exceptions. Normally a
|
||||
* {@link CompletionPolicy} will be used to decide whether to end a batch when
|
||||
* there is no exception, and the {@link ExceptionHandler} is used to signal an
|
||||
* abnormal ending - an abnormal ending would result in an
|
||||
* {@link ExceptionHandler} throwing an exception. The caller will catch and
|
||||
* rethrow it if necessary.
|
||||
* re-throw it if necessary.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Robert Kasanicky
|
||||
*
|
||||
*/
|
||||
public interface ExceptionHandler {
|
||||
|
||||
/**
|
||||
* Deal with a Throwable during a batch. The input might be
|
||||
* RuntimeException or other unchecked exceptions.
|
||||
* Deal with a Throwable during a batch - decide whether it should be
|
||||
* re-thrown in the first place.
|
||||
*
|
||||
* @param context
|
||||
* the current {@link RepeatContext}. Can be used to store state
|
||||
* (via attributes), for example to count the number of
|
||||
* occurrences of a particular exception type and implement a
|
||||
* threshold policy.
|
||||
* @param throwable
|
||||
* an exception.
|
||||
* @throws RuntimeException
|
||||
* implementations must wrap and rethrow other Throwables
|
||||
* appropriately.
|
||||
* @param context the current {@link RepeatContext}. Can be used to store
|
||||
* state (via attributes), for example to count the number of occurrences of
|
||||
* a particular exception type and implement a threshold policy.
|
||||
* @param throwable an exception.
|
||||
* @throws Throwable implementations are free to re-throw the exception
|
||||
*/
|
||||
void handleException(RepeatContext context, Throwable throwable)
|
||||
throws RuntimeException;
|
||||
void handleException(RepeatContext context, Throwable throwable) throws Throwable;
|
||||
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
|
||||
*
|
||||
* @see {@link ExceptionHandler#handleException(RepeatContext, Throwable)}
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
|
||||
|
||||
Object key = exceptionClassifier.classify(throwable);
|
||||
if (ERROR.equals(key)) {
|
||||
|
||||
@@ -107,15 +107,15 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify the throwables and decide whether to rethrow based on the
|
||||
* Classify the throwables and decide whether to re-throw based on the
|
||||
* result. The context is used to accumulate the number of exceptions of the
|
||||
* same type according to the classifier.
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws Throwable
|
||||
* @see {@link ExceptionHandler#handleException(RepeatContext, Throwable)}
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable)
|
||||
throws RuntimeException {
|
||||
throws Throwable {
|
||||
|
||||
Object key = exceptionClassifier.classify(throwable);
|
||||
RepeatContextCounter counter = getCounter(context, key);
|
||||
|
||||
@@ -105,7 +105,7 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler {
|
||||
* @see org.springframework.batch.repeat.exception.ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
|
||||
* Throwable)
|
||||
*/
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
|
||||
delegate.handleException(context, throwable);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public class CompositeExceptionHandlerTests extends TestCase {
|
||||
|
||||
private CompositeExceptionHandler handler = new CompositeExceptionHandler();
|
||||
|
||||
public void testNewHandler() throws Exception {
|
||||
public void testNewHandler() throws Throwable {
|
||||
try {
|
||||
handler.handleException(null, new RuntimeException());
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public class CompositeExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testDelegation() throws Exception {
|
||||
public void testDelegation() throws Throwable {
|
||||
final List list = new ArrayList();
|
||||
handler.setHandlers(new ExceptionHandler[] {
|
||||
new ExceptionHandler() {
|
||||
|
||||
@@ -25,7 +25,7 @@ public class DefaultExceptionHandlerTests extends TestCase {
|
||||
private DefaultExceptionHandler handler = new DefaultExceptionHandler();
|
||||
private RepeatContext context = null;
|
||||
|
||||
public void testRuntimeException() throws Exception {
|
||||
public void testRuntimeException() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new RuntimeException("Foo"));
|
||||
fail("Expected RuntimeException");
|
||||
@@ -34,7 +34,7 @@ public class DefaultExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testError() throws Exception {
|
||||
public void testError() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new Error("Foo"));
|
||||
fail("Expected Error");
|
||||
|
||||
@@ -43,7 +43,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
logger.addAppender(new WriterAppender(new SimpleLayout(), writer));
|
||||
}
|
||||
|
||||
public void testRuntimeException() throws Exception {
|
||||
public void testRuntimeException() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new RuntimeException("Foo"));
|
||||
fail("Expected RuntimeException");
|
||||
@@ -52,7 +52,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testError() throws Exception {
|
||||
public void testError() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new Error("Foo"));
|
||||
fail("Expected Error");
|
||||
@@ -61,7 +61,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNotRethrownErrorLevel() throws Exception {
|
||||
public void testNotRethrownErrorLevel() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return LogOrRethrowExceptionHandler.ERROR;
|
||||
@@ -72,7 +72,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
assertNotNull(writer.toString());
|
||||
}
|
||||
|
||||
public void testNotRethrownWarnLevel() throws Exception {
|
||||
public void testNotRethrownWarnLevel() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return LogOrRethrowExceptionHandler.WARN;
|
||||
@@ -83,7 +83,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
assertNotNull(writer.toString());
|
||||
}
|
||||
|
||||
public void testNotRethrownDebugLevel() throws Exception {
|
||||
public void testNotRethrownDebugLevel() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return LogOrRethrowExceptionHandler.DEBUG;
|
||||
@@ -94,7 +94,7 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
|
||||
assertNotNull(writer.toString());
|
||||
}
|
||||
|
||||
public void testUnclassifiedException() throws Exception {
|
||||
public void testUnclassifiedException() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return "DEFAULT";
|
||||
|
||||
@@ -31,7 +31,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
private RepeatContext parent = new RepeatContextSupport(null);
|
||||
private RepeatContext context = new RepeatContextSupport(parent);
|
||||
|
||||
public void testRuntimeException() throws Exception {
|
||||
public void testRuntimeException() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new RuntimeException("Foo"));
|
||||
fail("Expected RuntimeException");
|
||||
@@ -40,7 +40,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testError() throws Exception {
|
||||
public void testError() throws Throwable {
|
||||
try {
|
||||
handler.handleException(context, new Error("Foo"));
|
||||
fail("Expected Error");
|
||||
@@ -49,7 +49,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNotRethrownWithThreshold() throws Exception {
|
||||
public void testNotRethrownWithThreshold() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return "RuntimeException";
|
||||
@@ -63,7 +63,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
assertEquals(1, counter.getCount());
|
||||
}
|
||||
|
||||
public void testRethrowOnThreshold() throws Exception {
|
||||
public void testRethrowOnThreshold() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return "RuntimeException";
|
||||
@@ -92,7 +92,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNotUseParent() throws Exception {
|
||||
public void testNotUseParent() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return "RuntimeException";
|
||||
@@ -111,7 +111,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testUseParent() throws Exception {
|
||||
public void testUseParent() throws Throwable {
|
||||
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
|
||||
public Object classify(Throwable throwable) {
|
||||
return "RuntimeException";
|
||||
|
||||
@@ -36,7 +36,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
// object under test
|
||||
private SimpleLimitExceptionHandler handler = new SimpleLimitExceptionHandler();
|
||||
|
||||
public void testInitializeWithNullContext() throws Exception {
|
||||
public void testInitializeWithNullContext() throws Throwable {
|
||||
try {
|
||||
handler.handleException(null, new RuntimeException("foo"));
|
||||
fail("Expected IllegalArgumentException");
|
||||
@@ -45,7 +45,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInitializeWithNullContextAndNullException() throws Exception {
|
||||
public void testInitializeWithNullContextAndNullException() throws Throwable {
|
||||
try {
|
||||
handler.handleException(null, null);
|
||||
} catch (NullPointerException e) {
|
||||
@@ -58,7 +58,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testNormalExceptionThrown() throws Exception {
|
||||
public void testNormalExceptionThrown() throws Throwable {
|
||||
Throwable throwable = new RuntimeException("foo");
|
||||
|
||||
final int MORE_THAN_ZERO = 1;
|
||||
@@ -79,7 +79,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testLimitedExceptionTypeNotThrown() throws Exception {
|
||||
public void testLimitedExceptionTypeNotThrown() throws Throwable {
|
||||
final int MORE_THAN_ZERO = 1;
|
||||
handler.setLimit(MORE_THAN_ZERO);
|
||||
handler.setExceptionClasses(new Class[] {RuntimeException.class} );
|
||||
@@ -96,7 +96,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testLimitedExceptionNotThrownFromSiblings() throws Exception {
|
||||
public void testLimitedExceptionNotThrownFromSiblings() throws Throwable {
|
||||
Throwable throwable = new RuntimeException("foo");
|
||||
|
||||
final int MORE_THAN_ZERO = 1;
|
||||
@@ -120,7 +120,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testLimitedExceptionThrownFromSiblingsWhenUsingParent() throws Exception {
|
||||
public void testLimitedExceptionThrownFromSiblingsWhenUsingParent() throws Throwable {
|
||||
Throwable throwable = new RuntimeException("foo");
|
||||
|
||||
final int MORE_THAN_ZERO = 1;
|
||||
@@ -145,7 +145,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
* TransactionInvalidExceptions are swallowed until the exception limit is exceeded. After the limit is exceeded
|
||||
* exceptions are rethrown as BatchCriticalExceptions
|
||||
*/
|
||||
public void testExceptionNotThrownBelowLimit() throws Exception {
|
||||
public void testExceptionNotThrownBelowLimit() throws Throwable {
|
||||
|
||||
final int EXCEPTION_LIMIT = 3;
|
||||
handler.setLimit(EXCEPTION_LIMIT);
|
||||
@@ -178,7 +178,7 @@ public class SimpleLimitExceptionHandlerTests extends TestCase {
|
||||
* TransactionInvalidExceptions are swallowed until the exception limit is exceeded. After the limit is exceeded
|
||||
* exceptions are rethrown as BatchCriticalExceptions
|
||||
*/
|
||||
public void testExceptionThrownAboveLimit() throws Exception {
|
||||
public void testExceptionThrownAboveLimit() throws Throwable {
|
||||
|
||||
final int EXCEPTION_LIMIT = 3;
|
||||
handler.setLimit(EXCEPTION_LIMIT);
|
||||
|
||||
@@ -11,10 +11,10 @@ public class FootballExceptionHandler implements ExceptionHandler {
|
||||
.getLog(FootballExceptionHandler.class);
|
||||
|
||||
public void handleException(RepeatContext context, Throwable throwable)
|
||||
throws RuntimeException {
|
||||
throws Throwable {
|
||||
|
||||
if (!(throwable instanceof NumberFormatException)) {
|
||||
throw new RuntimeException(throwable);
|
||||
throw throwable;
|
||||
} else {
|
||||
logger.error("Number Format Exception!", throwable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user