From ce33690a89aa04c454ab1fbcd20cd4dbf16880e6 Mon Sep 17 00:00:00 2001 From: dsyer Date: Thu, 6 Mar 2008 18:14:20 +0000 Subject: [PATCH] OPEN - issue BATCH-409: StatefulRetryStepFactoryBean needs to co-ordinate exception handler with retry policy http://jira.springframework.org/browse/BATCH-409 Use retryLimit and exception types instead of retry policy - in the spirit of a factory bean being easy to configure for typical use cases. --- .../step/support/DefaultStepFactoryBean.java | 4 +- .../support/StatefulRetryStepFactoryBean.java | 44 ++++++++++++++++--- .../StatefulRetryStepFactoryBeanTests.java | 3 +- .../batch/retry/RetryPolicy.java | 4 +- .../src/main/resources/jobs/retrySample.xml | 22 +++------- 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultStepFactoryBean.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultStepFactoryBean.java index c870e1bb8..2e815dd28 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultStepFactoryBean.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultStepFactoryBean.java @@ -18,8 +18,8 @@ package org.springframework.batch.execution.step.support; import org.springframework.batch.core.domain.BatchListener; import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepListener; -import org.springframework.batch.execution.step.ItemOrientedStep; import org.springframework.batch.execution.step.ItemHandler; +import org.springframework.batch.execution.step.ItemOrientedStep; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemWriter; @@ -86,7 +86,7 @@ public class DefaultStepFactoryBean extends AbstractStepFactoryBean { public void setSkipLimit(int skipLimit) { this.skipLimit = skipLimit; } - + /** * The listeners to inject into the {@link Step}. Any instance of * {@link BatchListener} can be used, and will then receive callbacks at the diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBean.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBean.java index 5a8683e88..94a542f8a 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBean.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBean.java @@ -26,8 +26,10 @@ import org.springframework.batch.repeat.ExitStatus; import org.springframework.batch.repeat.exception.handler.SimpleLimitExceptionHandler; import org.springframework.batch.retry.RetryOperations; import org.springframework.batch.retry.RetryPolicy; +import org.springframework.batch.retry.backoff.BackOffPolicy; import org.springframework.batch.retry.callback.ItemReaderRetryCallback; import org.springframework.batch.retry.policy.ItemReaderRetryPolicy; +import org.springframework.batch.retry.policy.SimpleRetryPolicy; import org.springframework.batch.retry.support.RetryTemplate; /** @@ -44,18 +46,38 @@ import org.springframework.batch.retry.support.RetryTemplate; */ public class StatefulRetryStepFactoryBean extends DefaultStepFactoryBean { - private RetryPolicy retryPolicy; - private ItemKeyGenerator itemKeyGenerator; private ItemRecoverer itemRecoverer; + + private int retryLimit; + + private Class[] retryableExceptionClasses; + + private BackOffPolicy backOffPolicy; /** - * Public setter for the {@link RetryPolicy}. - * @param retryPolicy the {@link RetryPolicy} to set + * Public setter for the retry limit. Each item can be retried up to this limit. + * @param retryLimit the retry limit to set */ - public void setRetryPolicy(RetryPolicy retryPolicy) { - this.retryPolicy = retryPolicy; + public void setRetryLimit(int retryLimit) { + this.retryLimit = retryLimit; + } + + /** + * Public setter for the Class[]. + * @param retryableExceptionClasses the retryableExceptionClasses to set + */ + public void setRetryableExceptionClasses(Class[] retryableExceptionClasses) { + this.retryableExceptionClasses = retryableExceptionClasses; + } + + /** + * Public setter for the {@link BackOffPolicy}. + * @param backOffPolicy the {@link BackOffPolicy} to set + */ + public void setBackOffPolicy(BackOffPolicy backOffPolicy) { + this.backOffPolicy = backOffPolicy; } /** @@ -92,7 +114,12 @@ public class StatefulRetryStepFactoryBean extends DefaultStepFactoryBean { super.applyConfiguration(step); - if (retryPolicy != null) { + if (retryLimit>0) { + + SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(retryLimit); + if (retryableExceptionClasses!=null) { + retryPolicy.setRetryableExceptionClasses(retryableExceptionClasses); + } // TODO: actually we need to co-ordinate the retry policy with the // exception handler limit, so this is a hack for now. @@ -105,6 +132,9 @@ public class StatefulRetryStepFactoryBean extends DefaultStepFactoryBean { RetryTemplate retryTemplate = new RetryTemplate(); retryTemplate.setRetryPolicy(itemProviderRetryPolicy); + if (backOffPolicy!=null) { + retryTemplate.setBackOffPolicy(backOffPolicy); + } StatefulRetryItemHandler itemProcessor = new StatefulRetryItemHandler(getItemReader(), getItemWriter(), retryTemplate, retryCallback); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBeanTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBeanTests.java index 7b20df886..8dca0adf6 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBeanTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StatefulRetryStepFactoryBeanTests.java @@ -38,7 +38,6 @@ import org.springframework.batch.item.ItemRecoverer; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.reader.ListItemReader; import org.springframework.batch.item.writer.AbstractItemWriter; -import org.springframework.batch.retry.policy.AlwaysRetryPolicy; import org.springframework.batch.support.transaction.ResourcelessTransactionManager; import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; import org.springframework.transaction.support.TransactionSynchronizationManager; @@ -120,7 +119,7 @@ public class StatefulRetryStepFactoryBeanTests extends TestCase { } }; factory.setItemReader(provider); - factory.setRetryPolicy(new AlwaysRetryPolicy()); + factory.setRetryLimit(10); ItemOrientedStep step = (ItemOrientedStep) factory.getObject(); step.execute(new StepExecution(step, jobExecution)); 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 2acd7a331..fb37b0358 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 @@ -40,7 +40,7 @@ public interface RetryPolicy { /** * @param context the current context. * @return true if the policy determines that the last exception should be - * rethrown. + * re-thrown. */ boolean shouldRethrow(RetryContext context); @@ -57,7 +57,7 @@ public interface RetryPolicy { RetryContext open(RetryCallback callback); /** - * @param status a retry status crated by the {@link #open(RetryCallback)} + * @param status a retry status created by the {@link #open(RetryCallback)} * method of this manager. */ void close(RetryContext context); diff --git a/spring-batch-samples/src/main/resources/jobs/retrySample.xml b/spring-batch-samples/src/main/resources/jobs/retrySample.xml index b03ee2426..2e712dc11 100644 --- a/spring-batch-samples/src/main/resources/jobs/retrySample.xml +++ b/spring-batch-samples/src/main/resources/jobs/retrySample.xml @@ -1,8 +1,6 @@ - - - - - - - + + - + - + \ No newline at end of file