diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java index d0af039f4..9775fe436 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java @@ -24,6 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.retry.RetryCallback; import org.springframework.batch.retry.RetryContext; +import org.springframework.batch.retry.RetryException; import org.springframework.batch.retry.RetryListener; import org.springframework.batch.retry.RetryOperations; import org.springframework.batch.retry.RetryPolicy; @@ -177,7 +178,7 @@ public class RetryTemplate implements RetryOperations { if (retryPolicy.shouldRethrow(context)) { logger.debug("Abort retry for policy: count=" + context.getRetryCount()); - unwrapAndThrow(e); + rethrow(e); } } @@ -190,7 +191,7 @@ public class RetryTemplate implements RetryOperations { // back off was prevented by another thread - fail the // retry logger.debug("Abort retry because interrupted: count=" + context.getRetryCount()); - unwrapAndThrow(e); + rethrow(e); } /* @@ -244,13 +245,16 @@ public class RetryTemplate implements RetryOperations { } } - private void unwrapAndThrow(Throwable ex) throws Exception { + private static void rethrow(Throwable ex) throws Exception { if (ex instanceof Exception) { throw (Exception) ex; } else if (ex instanceof Error) { throw (Error) ex; } + else { + throw new RetryException("Unclassified Throwable encountered", ex); + } } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/support/RetryTemplateTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/support/RetryTemplateTests.java index ef3d02214..8c395976b 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/support/RetryTemplateTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/support/RetryTemplateTests.java @@ -21,6 +21,7 @@ import junit.framework.TestCase; import org.springframework.batch.retry.ExhaustedRetryException; import org.springframework.batch.retry.RetryCallback; import org.springframework.batch.retry.RetryContext; +import org.springframework.batch.retry.RetryException; import org.springframework.batch.retry.backoff.BackOffContext; import org.springframework.batch.retry.backoff.BackOffInterruptedException; import org.springframework.batch.retry.backoff.BackOffPolicy; @@ -230,6 +231,29 @@ public class RetryTemplateTests extends TestCase { } } + /** + * Throwables that aren't Exception nor Error are wrapped into + * RetryException. + */ + public void testThrowableWrapping() throws Exception { + RetryCallback callback = new RetryCallback() { + public Object doWithRetry(RetryContext context) throws Throwable { + throw new Throwable("throwable in callback"); + } + }; + RetryTemplate template = new RetryTemplate(); + + try { + template.execute(callback); + fail(); + } + catch (RetryException expected) { + expected.getMessage().equals("Unclassified Throwable encountered"); + expected.getCause().getMessage().equals("throwable in callback"); + } + + } + private static class MockRetryCallback implements RetryCallback { private int attempts;