简体   繁体   English

春季批:条件流

[英]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. 请注意:在Step1中,我使用的是纯tasklet方法。 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 ? 请帮忙,我该如何在Example tasklet中放入一些代码或进行一些配置,以决定要调用的下一步?

I've already looked into https://docs.spring.io/spring-batch/reference/html/configureStep.html 我已经看过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 然后在你的Tasklet ,通过实施设定的退出状态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");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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