Java Spring批处理在启动运行方法之前启动simplejoblancher运行方法
我的作业配置如下所示Java Spring批处理在启动运行方法之前启动simplejoblancher运行方法,java,spring-boot,spring-batch,Java,Spring Boot,Spring Batch,我的作业配置如下所示 @SpringBootApplication public class Test implements CommandLineRunner { @Autowired JobLauncher jobLauncher; @Autowired Job job; @Autowired private JobBuilderFactory jobs; @Autowired private StepBuilderFa
@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仅启动带有我需要的参数的作业您可以通过向application.yml或application.properties添加属性来禁用启动时自动执行作业
spring.batch.job.enabled: false
您的意思是在集成测试中禁用它,使用相同的属性spring.batch.job.enabled:false并将其放在项目的src/test/resources目录下的application.yml或application.properties中