2

I need to decide based on some condition in step1 of job, which step to call next.

Please note: in Step1, I'm using purely tasklet approach. Example:

<step id="step1">
   <tasklet ref="example">
</step>

Please help, how can I put some code in Example tasklet or make some configuration to decide the next step to be called ?

I've already looked into https://docs.spring.io/spring-batch/reference/html/configureStep.html

Dean Clark
  • 3,770
  • 1
  • 11
  • 26
B Talesra
  • 33
  • 1
  • 5

1 Answers1

4

You can dictate flow control in your context file like so:

<step id="step1">
    <tasklet ref="example">
    <next on="COMPLETED" to="step2" />
    <end on="NO-OP" />
    <fail on="*" />
    <!-- 
      You generally want to Fail on * to prevent 
      accidentally doing the wrong thing
    -->
</step>

Then in your Tasklet, set the ExitStatus by implementing StepExecutionListener

public class SampleTasklet implements Tasklet, StepExecutionListener {

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        // do something
        return RepeatStatus.FINISHED;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        // no-op
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        //some logic here
        boolean condition1 = false;
        boolean condition2 = true;

        if (condition1) {
            return new ExitStatus("COMPLETED");
        } else if (condition2) {
            return new ExitStatus("FAILED"); 
        }

        return new ExitStatus("NO-OP");
    }

}
Dean Clark
  • 3,770
  • 1
  • 11
  • 26