Spring – how to control repeat and next step execution in spring batch

springspring-batch

Hi I have below like xml for executing Job

<batch:job id="Job1" restartable="false" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1" next="step2">
            <tasklet ref="automate" />
        </step>
        <step id="step2">
            <tasklet ref="drive"  />
            <next on="COMPLETED" to="step3"></next>
        </step>
        <step id="step3">
            <tasklet ref="generate_file"  />
        </step>
    </batch:job>

For this I have write a tasklet to execute a script. Now I want that if script execution failed three times then next step will not execute . But from Tasklet I am able to return only Finished which move the flow to next step and continuable which continue the process. What should I do in this.

Best Answer

you can write your own decider to decide wheather to goto next step or to end the job. if you are able to handle the failures you can also handle the flow of a job

<decision id="validationDecision" decider="validationDecider">
        <next on="FAILED" to="abcStep" />
        <next on="COMPLETE" to="xyzstep" />
    </decision>

config is

<bean id="validationDecider" class="com.xyz.StepFlowController" />

class is

public class StepFlowController implements JobExecutionDecider{

@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
    FlowExecutionStatus status = null;
    try {
        if (failure) {
status = new FlowExecutionStatus("FAILED");

        }else {
            status = new FlowExecutionStatus("COMPLETE");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return status;
}
Related Topic