春季批处理作业执行同一决定者两次

时间:2018-10-25 19:43:56

标签: spring-boot spring-batch

我要使用此流程创建工作:

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时,它会运行所有步骤,就像第二次运行决定程序时一样创建和更新,同时选择两条路径。我在做什么错了?

2 个答案:

答案 0 :(得分:2)

对我很好。

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

一次运行无法获得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();
    }
}

答案 1 :(得分:0)

由于这仅发生在单元测试中,所以我更改了运行方式,现在可以正常工作了。