简体   繁体   中英

spring batch : Conditional Flow

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

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");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM