导致作业退出状态失败的Spring Batch条件流程

时间:2019-03-01 08:48:48

标签: java spring-batch

我在执行以下操作(简化)时遇到了弹簧批处理流程的问题

  1. 始终执行一个步骤
  2. 在运行时决定下一步该做什么(jobexecutiondecider)
  3. 如果决策者决定“继续”->执行更复杂的内部流程
  4. 如果决策者确定要“完成”->不执行内部流程,而只是完成工作

给出以下MVE:

@Configuration
@EnableBatchProcessing
public class BatchExample {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job exampleJob() {
        final FlowBuilder<Flow> innerFlowBuilder = new FlowBuilder<>("innerFlow");
        final Flow innerFlow = innerFlowBuilder
            .start(dummyStep("first innerflow step"))
            .next(dummyStep("second innerflow step"))
            .next(dummyStep("last innerflow step"))
            .build();

        final FlowBuilder<Flow> outerFlowBuilder = new FlowBuilder<>("outerFlow");
        final Flow outerFlow = outerFlowBuilder
            .start(dummyStep("always execute me"))
            .next(decide()).on("CONTINUE").to(innerFlow)
            .from(decide()).on("COMPLETED").end("COMPLETED")
            .build();

        return jobBuilderFactory.get("exampleJob")
            .start(outerFlow)
            .end()
            .build();
    }

    private Step dummyStep(String arg) {
        return stepBuilderFactory.get("step_" + arg)
            .tasklet(dummyTasklet(arg))
            .build();
    }

    private Tasklet dummyTasklet(String arg) {
        return new DummyTasklet(arg);
    }

    private DummyDecider decide() {
        return new DummyDecider();
    }

    class DummyTasklet implements Tasklet {

        private final String arg;

        DummyTasklet(String arg) {
            this.arg = arg;
        }

        @Override
        public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
            System.out.println("hello from dummy tasklet: " + arg);
            return RepeatStatus.FINISHED;
        }
    }

    class DummyDecider implements JobExecutionDecider {

        @Override
        public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
            final Random random = new Random();
            final int i = random.nextInt();

            if (i % 2 == 0) {
                return new FlowExecutionStatus("CONTINUE");
            } else {
                return FlowExecutionStatus.COMPLETED;
            }
        }
    }    
}

执行代码时,“继续”流程进行得很顺利,但“完成”流程始终会以失败的作业状态退出。

 Job: [FlowJob: [name=exampleJob]] completed with the following
 parameters: [{}] and the following status: [FAILED]

如何使工作完成并显示为COMPLETED状态?换句话说,我对流的编码做错了什么?

该代码可以在Spring Boot应用程序中运行

@SpringBootApplication
public class FlowApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(FlowApplication.class, args);
    }

    @Autowired
    private Job exampleJob;
    @Autowired
    private JobLauncher jobLauncher;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        jobLauncher.run(exampleJob, new JobParameters());
    }
}

修改

实施建议的答案时

final Flow outerFlow = outerFlowBuilder
            .start(dummyStep("always execute me"))
            .on("*").to(decide())
            .from(decide()).on("CONTINUE").to(innerFlow)
            .from(decide()).on("COMPLETED").end("COMPLETED")
            .build();

并迫使决策者“继续”:

class DummyDecider implements JobExecutionDecider {
        @Override
        public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
            return new FlowExecutionStatus("CONTINUE");
        }
    }

内部流程不再启动:

Executing step: [step_always execute me]
hello from dummy tasklet: always execute me
Job: [FlowJob: [name=exampleJob]] completed with the following parameters: [{}] and the following status: [FAILED]
Job: [FlowJob: [name=exampleJob]] launched with the following parameters: [{}]
Step already complete or not restartable, so no action to execute: StepExecution: id=0, version=3, name=step_always execute me, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
Job: [FlowJob: [name=exampleJob]] completed with the following parameters: [{}] and the following status: [FAILED]

1 个答案:

答案 0 :(得分:0)

您的outerFlow应该定义如下:

final Flow outerFlow = outerFlowBuilder
        .start(dummyStep("always execute me"))
        .on("*").to(decide())
        .from(decide()).on("CONTINUE").to(innerFlow)
        .from(decide()).on("COMPLETED").end("COMPLETED")
        .build();

我用您的示例对此进行了测试(谢谢您的MVE!),当决策者返回COMPLETED时,工作不会失败。

原因是,如果没有将dummyStep("always execute me")步骤的所有结果路由到.on("*").to(decide())到决策者,则流程定义不正确。

希望这会有所帮助。