Spring 在启动前批量启动 SimpleJobLauncher 运行 方法 运行 方法
Spring batch launches SimpleJobLauncher run method before boot run method
我的作业配置如下
@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 批处理只启动带有我需要的参数的作业
您可以通过将 属性 添加到 application.yml 或 application.properties
来禁用启动时自动执行作业
spring.batch.job.enabled: false
当您有多个作业时,您必须在批处理配置中定义多个作业 bean。您可以自动装配两个 Job bean,然后根据您可以执行的参数将正确的 bean 传递给 运行()
@Autowired
private Job importUserJob;
@Autowired
private Job syncUserJob;
if (jobNameArgument.equals("importUserJob")){
jobLauncher.run(importUserJob, params);
}else {
jobLauncher.run(syncUserJob, params);
}
我的作业配置如下
@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 批处理只启动带有我需要的参数的作业
您可以通过将 属性 添加到 application.yml 或 application.properties
来禁用启动时自动执行作业spring.batch.job.enabled: false
当您有多个作业时,您必须在批处理配置中定义多个作业 bean。您可以自动装配两个 Job bean,然后根据您可以执行的参数将正确的 bean 传递给 运行()
@Autowired
private Job importUserJob;
@Autowired
private Job syncUserJob;
if (jobNameArgument.equals("importUserJob")){
jobLauncher.run(importUserJob, params);
}else {
jobLauncher.run(syncUserJob, params);
}