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.
This commit is contained in:
dsyer
2008-03-06 18:14:20 +00:00
parent 1a48321d37
commit ce33690a89
5 changed files with 48 additions and 29 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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));