首页 文章

spring 批次:条件流程

提问于
浏览
2

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

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

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

请帮忙,我如何在Example tasklet中添加一些代码或进行一些配置来决定下一步调用?

我已经调查了https://docs.spring.io/spring-batch/reference/html/configureStep.html

1 回答

  • 3

    您可以在上下文文件中指定流控制,如下所示:

    <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 设置ExitStatus

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

相关问题