【问题标题】:Spring batch launches SimpleJobLauncher run method before boot run methodSpring批处理在启动运行方法之前启动SimpleJobLauncher运行方法
【发布时间】:2020-10-07 00:12:32
【问题描述】:

我的工作配置如下

@SpringBootApplication
public class Test implements CommandLineRunner {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

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

    @Override
    public void run(String... args) throws Exception {
        JobParameters params = new JobParametersBuilder()
                .addString("JobID", String.valueOf(System.currentTimeMillis()))
                .toJobParameters();
        jobLauncher.run(job, params);
    }
}

现在,问题是当我运行这个测试应用程序时,SimpleJobLauncher 在创建 JobParameters 之前启动运行方法。来自日志

10:12:58.422 - [    main] - INFO  SimpleJobLauncher                        - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{}]
10:12:58.466 - [    main] - INFO  SimpleStepHandler                        - Step already complete or not restartable, so no action to execute: StepExecution: id=14, version=3, name=stepOne, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
10:12:58.478 - [    main] - INFO  SimpleStepHandler                        - Step already complete or not restartable, so no action to execute: StepExecution: id=15, version=3, name=stepTwo, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
10:12:58.498 - [    main] - INFO  SimpleJobLauncher                        - Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 44ms
10:12:58.530 - [    main] - INFO  SimpleJobLauncher                        - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1592381578499}]

从日志中可以看出,第一个 demoJob 是在没有参数的情况下启动的

10:12:58.422 - [    main] - INFO  SimpleJobLauncher                        - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{}]

在没有参数的情况下完成作业后,它会再次使用参数启动。

10:12:58.530 - [    main] - INFO  SimpleJobLauncher                        - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1592381578499}]

假设应用程序中有两个作业,那么即使我想用指定的参数运行一个特定的作业,这两个作业也会启动 有没有办法控制这种行为,所以 Spring 批处理只启动带有我需要的参数的作业

【问题讨论】:

    标签: java spring-boot spring-batch


    【解决方案1】:

    您可以通过向 application.yml 或 application.properties 添加属性来禁用启动时自动执行作业

    spring.batch.job.enabled: false
    

    【讨论】:

    • 您的意思是在集成测试中禁用它,使用相同的属性 spring.batch.job.enabled: false 并将其放在 src/test/resources 目录中的 application.yml 或 application.properties你的项目
    【解决方案2】:

    当您有多个作业时,您必须在批处理配置中定义多个作业 bean。您可以自动装配两个 Job bean,然后根据参数执行将正确的 bean 传递给 run()

    @Autowired
    private Job importUserJob;
    
    @Autowired
    private Job syncUserJob;
    
    if (jobNameArgument.equals("importUserJob")){
            jobLauncher.run(importUserJob, params);
    }else {
            jobLauncher.run(syncUserJob, params);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多