OPEN - issue BATCH-404: FactoryBeans for step configuration

http://jira.springframework.org/browse/BATCH-404

New flag on DefaultStepFactoryBean for item skip policy (alwaysSkip=false is the default).
This commit is contained in:
dsyer
2008-03-02 12:04:12 +00:00
parent df0c615ff8
commit 108c21b187
4 changed files with 60 additions and 43 deletions

View File

@@ -16,17 +16,20 @@
package org.springframework.batch.execution.step.support;
import org.springframework.batch.execution.step.ItemOrientedStep;
import org.springframework.batch.repeat.exception.handler.SimpleLimitExceptionHandler;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.batch.repeat.support.RepeatTemplate;
/**
* @author Dave Syer
*
*
*/
public class SimpleStepFactoryBean extends AbstractStepFactoryBean {
private int commitInterval = 0;
private boolean alwaysSkip = false;
/**
* Set the commit interval.
*
@@ -36,19 +39,45 @@ public class SimpleStepFactoryBean extends AbstractStepFactoryBean {
this.commitInterval = commitInterval;
}
/**
* Public setter for the flag that determines skip policy. If this flag is
* true then an exception in chunk processing will cause the item to be
* skipped and no exceptions propagated. If it is false then all exceptions
* will be propagated from the chunk and cause the step to abort.
*
* @param alwaysSkip the value to set. Default is false.
*/
public void setAlwaysSkip(boolean alwaysSkip) {
this.alwaysSkip = alwaysSkip;
}
/**
* @param step
*
*/
protected void applyConfiguration(ItemOrientedStep step) {
super.applyConfiguration(step);
if (commitInterval > 0) {
RepeatTemplate chunkOperations = new RepeatTemplate();
chunkOperations.setCompletionPolicy(new SimpleCompletionPolicy(commitInterval));
step.setChunkOperations(chunkOperations);
}
if (alwaysSkip) {
// If we always skip (not the default) then we are prepared to
// absorb all exceptions at the step level because the failed items
// will never re-appear after a rollback.
step.setItemSkipPolicy(new AlwaysSkipItemSkipPolicy());
RepeatTemplate stepOperations = new RepeatTemplate();
stepOperations.setExceptionHandler(new SimpleLimitExceptionHandler(Integer.MAX_VALUE));
step.setStepOperations(stepOperations);
}
else {
// This is the default in ItemOrientedStep anyway...
step.setItemSkipPolicy(new NeverSkipItemSkipPolicy());
}
}
}

View File

@@ -45,14 +45,22 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler {
* Set this flag to true if you want to count exceptions for the whole
* (outer) loop in a typical container.
*
* @param useParent
* true if the parent context should be used to store the
* counters.
* @param useParent true if the parent context should be used to store the
* counters.
*/
public void setUseParent(boolean useParent) {
delegate.setUseParent(useParent);
}
/**
* Convenience constructor for the {@link SimpleLimitExceptionHandler} to
* set the limit.
*/
public SimpleLimitExceptionHandler(int limit) {
this();
setLimit(limit);
}
/**
* Default constructor for the {@link SimpleLimitExceptionHandler}.
*/
@@ -76,10 +84,9 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler {
* @see #setLimit(int)
*
* @see org.springframework.batch.repeat.exception.handler.ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
* Throwable)
* Throwable)
*/
public void handleException(RepeatContext context, Throwable throwable)
throws RuntimeException {
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
delegate.handleException(context, throwable);
}

View File

@@ -15,31 +15,17 @@
<!-- set restartable=false so that this job can be used by more than one test -->
<property name="restartable" value="false" />
<property name="steps">
<bean id="step1" parent="defaultStep"
class="org.springframework.batch.execution.step.support.KitchenSinkStepFactoryBean">
<property name="itemReader" ref="hibernateItemReader" />
<property name="itemWriter" ref="hibernateOutputSource" />
<property name="commitInterval" value="3" />
<property name="exceptionHandler">
<bean
class="org.springframework.batch.repeat.exception.handler.SimpleLimitExceptionHandler">
<property name="limit" value="5" />
<property name="useParent" value="true" />
</bean>
</property>
<property name="itemSkipPolicy">
<bean
class="org.springframework.batch.execution.step.support.AlwaysSkipItemSkipPolicy" />
</property>
</bean>
</property>
</bean>
<!-- This is a framework class that needs a delegate and also needs to be registered as a RepeatInterceptor in the chunk -->
<bean id="step1" parent="defaultStep">
<property name="alwaysSkip" value="true" />
<property name="itemReader" ref="hibernateItemReader" />
<property name="itemWriter" ref="hibernateOutputSource" />
<property name="commitInterval" value="3" />
</bean>
</property>
</bean>
<!-- This is a framework class that needs a delegate and also needs to be registered as a RepeatInterceptor in the chunk -->
<bean id="hibernateOutputSource"
class="org.springframework.batch.io.support.HibernateAwareItemWriter">
<property name="sessionFactory" ref="sessionFactory" />

View File

@@ -14,15 +14,10 @@
<bean id="tradeJob" parent="simpleJob">
<property name="steps">
<list>
<bean id="step1" parent="simpleStep"
class="org.springframework.batch.execution.step.support.KitchenSinkStepFactoryBean">
<property name="exceptionHandler">
<bean
class="org.springframework.batch.repeat.exception.handler.SimpleLimitExceptionHandler"
p:limit="5" />
</property>
<property name="listeners" ref="fileInputTemplate" />
<property name="itemReader">
<bean id="step1" parent="defaultStep">
<property name="alwaysSkip" value="true" />
<property name="listeners" ref="fileInputTemplate" />
<property name="itemReader">
<bean
class="org.springframework.batch.item.reader.ValidatingItemReader">
<property name="itemReader"