繁体   English   中英

春季批:条件流

[英]spring batch : Conditional Flow

我需要根据工作的第一步中的某些条件来决定,接下来要调用哪一步。

请注意:在Step1中,我使用的是纯tasklet方法。 例:

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

请帮忙,我该如何在Example tasklet中放入一些代码或进行一些配置,以决定要调用的下一步?

我已经看过https://docs.spring.io/spring-batch/reference/html/configureStep.html

您可以像这样在上下文文件中指定流控制:

<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>

然后在你的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