【问题标题】:Spring batch job executes twice the same deciderSpring 批处理作业执行两次相同的决策程序
【发布时间】:2018-10-25 19:43:55
【问题描述】:

我想用这个流程创建一个工作:

execute step1
if (resource doesn't exist)
   execute createStep
else
   execute updateStep

我创建了一个决策程序来返回“CREATE”或“UPDATE”。我的决策者是用 @Service 定义的,所以 bean 将被自动装配

我的工作流程如下:

return jobs.get("someJobName")
       .start(step1())
       .next(myDecider).on("CREATE").to(createStep())
       .from(myDecider).on("UPDATE").to(updateStep())
       .end().build();

当我运行我的 UnitTest 时,它会运行所有步骤、创建和更新,就像它第二次选择两条路径运行决策程序一样。我做错了什么?

【问题讨论】:

    标签: spring-boot spring-batch


    【解决方案1】:

    我觉得不错。

    下面是一个与您的工作流程保持相同结构的单类工作示例。

    不可能在一次运行中同时获得CREATEUPDATE 的系统退出消息。

    将其与您的设置进行比较,如果您仍然遇到问题,请在说明中包含运行代码。

    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.Step;
    import org.springframework.batch.core.StepContribution;
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    import org.springframework.batch.core.job.flow.FlowExecutionStatus;
    import org.springframework.batch.core.job.flow.JobExecutionDecider;
    import org.springframework.batch.core.scope.context.ChunkContext;
    import org.springframework.batch.repeat.RepeatStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    import java.util.Random;
    
    @EnableBatchProcessing
    @SpringBootApplication
    public class DemoApplication {
    
        @Autowired
        StepBuilderFactory stepBuilderFactory;
    
        @Autowired
        JobBuilderFactory jobBuilderFactory;
    
        @Bean
        MyDecider myDecider() {
            return new MyDecider();
        }
    
        @Bean
        Step stepInit() {
            return stepFactoryWithMessage("stepInit");
        }
    
        @Bean
        Step createStep() {
            return stepFactoryWithMessage("createStep");
        }
    
        @Bean
        Step updateStep() {
            return stepFactoryWithMessage("updateStep");
        }
    
        @Bean
        Job job() {
            return this.jobBuilderFactory.get("job")
                    .start(stepInit())
                    .next(myDecider()).on("CREATE").to(createStep())
                    .from(myDecider()).on("UPDATE").to(updateStep())
                    .end()
                    .build();
        }
    
        static class MyDecider implements JobExecutionDecider {
            public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
                String status;
                if (new Random().nextBoolean()) {
                    status = "CREATE";
                }
                else {
                    status = "UPDATE";
                }
                return new FlowExecutionStatus(status);
            }
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    
        private Step stepFactoryWithMessage(String stepName) {
            return this.stepBuilderFactory.get(stepName)
                    .tasklet(
                            (StepContribution stepContribution, ChunkContext chunkContext) -> {
                                System.out.println("from " + stepName);
                                return RepeatStatus.FINISHED;
                            }).build();
        }
    }
    

    【讨论】:

    • 我支持这个答案。根据问题中的要求进行了测试并按预期工作。
    • 是的。当我运行启动应用程序时,它只运行一个小任务。这仅在我在单元测试中运行时出现。 JobExecution jobExecution = jobLauncherTestUtils.launchJob() 对此有何提示?
    【解决方案2】:

    由于这只发生在单元测试中,我改变了运行它们的方式,现在它可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 2013-10-21
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多