配置自动运行
spring.batch.job.enabled=true
spring.batch.job.names=job1,job2,job3
spring.batch.job.enabled
- true 开启Job自动执行
- false 关闭Job自动执行
spring.batch.job.names
- 自动执行的Job名称
BatchAutoConfiguration
如果spring.batch.job.enabled
为true 那么会初始化JobLauncherCommandLineRunner
,同时将job.names设置进去
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(
prefix = "spring.batch.job",
name = {"enabled"},
havingValue = "true",
matchIfMissing = true
)
public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer) {
JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(jobLauncher, jobExplorer);
String jobNames = this.properties.getJob().getNames();
if(StringUtils.hasText(jobNames)) {
runner.setJobNames(jobNames);
}
return runner;
}
private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecutionException {
if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
for (String jobName : jobsToRun) {
try {
Job job = this.jobRegistry.getJob(jobName);
if (this.jobs.contains(job)) {
continue;
}
execute(job, jobParameters);
}
catch (NoSuchJobException ex) {
logger.debug("No job found in registry for job name: " + jobName);
}
}
}
}