【发布时间】: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