Spring-Batch: how do I return a custom Job exit STATUS from a StepListener to decide next step

spring, spring-batch

Solution

They are several ways to do this.

You can use a `StepExecutionListener` and override the `afterStep` method:

@AfterStep
public ExitStatus afterStep(){
    //Test condition
    return new ExistStatus("CUSTOM EXIT STATUS");
}

Or use a `JobExecutionDecider` to choose the next step based on a result.

public class CustomDecider implements JobExecutionDecider  {

    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        if (/* your conditon */) {
            return new FlowExecutionStatus("OK");
        }
        return new FlowExecutionStatus("OTHER CODE HERE");
    }

}

Xml config:

    <decision id="decider" decider="decider">
        <next on="OK" to="step1" />
        <next on="OHTER CODE HERE" to="step2" />
    </decision>

<bean id="decider" class="com.xxx.CustomDecider"/>

Problem

The issue is this: I have a Spring Batch job with multiple step. Based on step one i have to decide the next steps. Can i set status in STEP1- passTasklet based on a job parameter so that i can set the Exit status to a custom status and define it in the job definition file to go to which next step. ``` Example <job id="conditionalStepLogicJob"> <step id="step1"> <tasklet ref="passTasklet"/> <next on="BABY" to="step2a"/> <stop on="KID" to="step2b"/> <next on="*" to="step3"/> </step> <step id="step2b"> <tasklet ref="kidTasklet"/> </step> <step id="step2a"> <tasklet ref="babyTasklet"/> </step> <step id="step3"> <tasklet ref="babykidTasklet"/> </step> </job> ``` i ideally want my own EXIT STATUS to be used between steps. Can i do that? will it not break any OOTB flow? is it valid to do

Original source