From 0bf473215cb7aacaf0c8e7ed156c1cbe302cdac8 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 10 May 2008 15:40:56 +0000 Subject: [PATCH] IN PROGRESS - issue BATCH-569: Add RetryOperationsInterceptor with stateful retry Tidied up exception handling and signatures of callbacks. --- .../org/springframework/batch/retry/RecoveryCallback.java | 5 ++++- .../org/springframework/batch/retry/RetryCallback.java | 1 - .../java/org/springframework/batch/retry/RetryPolicy.java | 4 ++-- .../batch/retry/policy/AbstractStatefulRetryPolicy.java | 3 ++- .../batch/retry/policy/AbstractStatelessRetryPolicy.java | 2 +- .../batch/retry/policy/ExceptionClassifierRetryPolicy.java | 2 +- .../batch/retry/policy/RecoveryCallbackRetryPolicy.java | 7 +++++-- .../src/main/resources/batch-hsql.properties | 4 ++-- .../src/main/resources/jobs/fixedLengthImportJob.xml | 2 +- 9 files changed, 18 insertions(+), 12 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RecoveryCallback.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RecoveryCallback.java index 9157c12a0..8d03988e3 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RecoveryCallback.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RecoveryCallback.java @@ -16,13 +16,16 @@ package org.springframework.batch.retry; /** + * Callback for stateful retry after all tries are exhausted. + * * @author Dave Syer * + * @since 1.1 */ public interface RecoveryCallback { /** - * @param throwable + * @param throwable the cause of the failure that we are to recover from * @return an Object that can be used to replace the callback result that * failed */ diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryCallback.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryCallback.java index 79c3a4339..0681b2e41 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryCallback.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryCallback.java @@ -20,7 +20,6 @@ package org.springframework.batch.retry; * Callback interface for an operation that can be retried using a * {@link RetryOperations}. * - * @since 2.1 * @author Rob Harrop */ public interface RetryCallback { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryPolicy.java index bc5b0419f..18b234977 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/RetryPolicy.java @@ -79,7 +79,7 @@ public interface RetryPolicy { * @param context the current retry context. * @return an appropriate value possibly from the callback. * - * @throws Exception if there is no recovery path. + * @throws ExhaustedRetryException if there is no recovery path. */ - Object handleRetryExhausted(RetryContext context) throws Exception, ExhaustedRetryException; + Object handleRetryExhausted(RetryContext context) throws ExhaustedRetryException; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatefulRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatefulRetryPolicy.java index b764585b8..982f61f2d 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatefulRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatefulRetryPolicy.java @@ -20,6 +20,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import org.springframework.batch.retry.ExhaustedRetryException; import org.springframework.batch.retry.RetryContext; import org.springframework.batch.retry.RetryPolicy; @@ -60,7 +61,7 @@ public abstract class AbstractStatefulRetryPolicy implements RetryPolicy { * * @see org.springframework.batch.retry.RetryPolicy#handleRetryExhausted(org.springframework.batch.retry.RetryContext) */ - public Object handleRetryExhausted(RetryContext context) throws Exception { + public Object handleRetryExhausted(RetryContext context) throws ExhaustedRetryException { return null; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatelessRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatelessRetryPolicy.java index 54a056a75..7c92418f1 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatelessRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/AbstractStatelessRetryPolicy.java @@ -49,7 +49,7 @@ public abstract class AbstractStatelessRetryPolicy implements RetryPolicy { * * @see org.springframework.batch.retry.RetryPolicy#handleRetryExhausted(org.springframework.batch.retry.RetryContext) */ - public Object handleRetryExhausted(RetryContext context) throws Exception, ExhaustedRetryException { + public Object handleRetryExhausted(RetryContext context) throws ExhaustedRetryException { throw new ExhaustedRetryException("Retry exhausted after last attempt with no recovery path.", context .getLastThrowable()); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java index dd819fabf..6ad2282ae 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java @@ -176,7 +176,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy return result; } - public Object handleRetryExhausted(RetryContext context) throws Exception { + public Object handleRetryExhausted(RetryContext context) throws UnsupportedOperationException { // Not called... throw new UnsupportedOperationException("Not supported - this code should be unreachable."); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/RecoveryCallbackRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/RecoveryCallbackRetryPolicy.java index e9a3ed07b..345f60d03 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/RecoveryCallbackRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/RecoveryCallbackRetryPolicy.java @@ -19,6 +19,7 @@ package org.springframework.batch.retry.policy; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.repeat.support.RepeatSynchronizationManager; +import org.springframework.batch.retry.ExhaustedRetryException; import org.springframework.batch.retry.RecoveryCallback; import org.springframework.batch.retry.RetryCallback; import org.springframework.batch.retry.RetryContext; @@ -127,7 +128,7 @@ public class RecoveryCallbackRetryPolicy extends AbstractStatefulRetryPolicy { * * @see org.springframework.batch.retry.policy.AbstractStatefulRetryPolicy#handleRetryExhausted(org.springframework.batch.retry.RetryContext) */ - public Object handleRetryExhausted(RetryContext context) throws Exception { + public Object handleRetryExhausted(RetryContext context) throws ExhaustedRetryException { return ((RetryPolicy) context).handleRetryExhausted(context); } @@ -204,13 +205,15 @@ public class RecoveryCallbackRetryPolicy extends AbstractStatefulRetryPolicy { throw new UnsupportedOperationException("Not supported - this code should be unreachable."); } - public Object handleRetryExhausted(RetryContext context) throws Exception { + public Object handleRetryExhausted(RetryContext context) throws ExhaustedRetryException { // If there is no going back, then we can remove the history retryContextCache.remove(key); RepeatSynchronizationManager.setCompleteOnly(); if (recoverer != null) { return recoverer.recover(context.getLastThrowable()); } + logger.info("No recover callback provided. Returning null from recovery step."); + // Don't want to call the delegate here - it would throw an exception return null; } diff --git a/spring-batch-samples/src/main/resources/batch-hsql.properties b/spring-batch-samples/src/main/resources/batch-hsql.properties index 7f28d4f4a..fae546bb8 100644 --- a/spring-batch-samples/src/main/resources/batch-hsql.properties +++ b/spring-batch-samples/src/main/resources/batch-hsql.properties @@ -1,10 +1,10 @@ # Placeholders batch.* # for HSQLDB: batch.jdbc.driver=org.hsqldb.jdbcDriver -batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true +# batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true # use this one for a separate server process so you can inspect the results # (or add it to system properties with -D to override at run time). -# batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples +batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples batch.jdbc.user=sa batch.jdbc.password= batch.schema= diff --git a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml index d5ec6ff99..a57a6f840 100644 --- a/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/fixedLengthImportJob.xml @@ -11,7 +11,7 @@ - +