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

View File

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

View File

@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
@@ -15,24 +13,16 @@
class="org.springframework.batch.execution.step.support.StatefulRetryStepFactoryBean">
<property name="itemReader" ref="itemGenerator" />
<property name="itemWriter" ref="itemWriter" />
<property name="retryPolicy">
<bean
class="org.springframework.batch.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="3" />
<property name="retryableExceptionClasses"
value="java.lang.Exception" />
</bean>
</property>
<property name="retryLimit" value="3" />
<property name="retryableExceptionClasses" value="java.lang.Exception" />
</bean>
</property>
</bean>
<bean id="itemGenerator"
class="org.springframework.batch.sample.item.reader.GeneratingItemReader">
<bean id="itemGenerator" class="org.springframework.batch.sample.item.reader.GeneratingItemReader">
<property name="limit" value="10" />
</bean>
<bean id="itemWriter"
class="org.springframework.batch.sample.item.writer.RetrySampleItemWriter" />
<bean id="itemWriter" class="org.springframework.batch.sample.item.writer.RetrySampleItemWriter" />
</beans>