有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring批处理在启动运行方法之前启动SimpleZoblancher运行方法

我的工作配置如下

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

现在的问题是,当我运行这个测试应用程序时,SimpleZoBlancher会在创建JobParameters之前启动run方法。从日志

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 batch只启动带有我需要的参数的作业


共 (2) 个答案

  1. # 1 楼答案

    通过向应用程序添加属性,可以在启动时禁用作业的自动执行。yml或应用程序。属性

    spring.batch.job.enabled: false
    
  2. # 2 楼答案

    当您有多个作业时,必须在批处理配置中定义多个作业bean。您可以自动连接两个作业bean,然后根据参数执行pass correct bean to run()

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