2
votes

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.

2
Check about retryLuca Basso Ricci
Take a look at the SystemCommandTasklet. It provides the facilities to execute a script as well as ways to handle errors that come back.Michael Minella

2 Answers

4
votes

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;
}
0
votes

This can be achieved by specifying a custom "chunk-completion-policy" in that step and count the number of failures. Take a look at "Stopping a Job Manually for Business Reasons" and this example for custom chunk completion policy. Hope this helps.

EDIT: you can put the number of failures in step execution context like in your step logic and then retrieve it in your completion policy class: stepExecution.getJobExecution().getExecutionContext().put("ERROR_COUNT", noOfErrors);