Java Reference
In-Depth Information
package com.apress.springbatch.chapter4;
import java.util.Random;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
public class RandomDecider implements JobExecutionDecider {
private Random random = new Random();
public FlowExecutionStatus decide(JobExecution jobExecution,
StepExecution stepExecution) {
if (random.nextBoolean()) {
return new
FlowExecutionStatus(FlowExecutionStatus.COMPLETED.getName());
} else {
return new
FlowExecutionStatus(FlowExecutionStatus.FAILED.getName());
}
}
}
To use RandomDecider , you configure an extra attribute on your step called decider . This attribute
refers to the Spring bean that implements JobExecutionDecider . Listing 4-42 shows RandomDecider
configured. You can see that the configuration maps the values you return in the decider to steps
available to execute.
Listing 4-42. If / Else Logic in Step Execution
...
<beans:bean id="decider"
class="com.apress.springbatch.chapter4.RandomDecider"/>
<beans:bean id="successTasklet"
class="com.apress.springbatch.chapter4.MessageTasklet">
<beans:property name="message" value="The step succeeded!"/>
</beans:bean>
<beans:bean id="failTasklet"
class="com.apress.springbatch.chapter4.MessageTasklet">
<beans:property name="message" value="The step failed!"/>
</beans:bean>
<job id="conditionalLogicJob">
<step id="step1" next="decision">
<tasklet>
<chunk reader="inputReader" writer="outputWriter"
commit-interval="20"/>
 
Search WWH ::




Custom Search