Spring Batch: Running Batch Jobs Asynchronously
When running multiple batch jobs in Spring Batch, the default JobLauncher waits to launch a job until the previous job running is COMPLETE. In order to run multiple batch jobs simultaneously, the JobLauncher must be configured to run jobs asynchronously.
Configure the async job launcher
public JobLauncher customJobLauncher(@Autowired final JobRepository jobRepository) {
final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
// Here we configure the async task executer
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
Run a job with the asyc job launcher
public class BatchJobScheduler {
@Autowired
private JobLauncher customJobLauncher;
@Autowired
private Job customJob;
@Scheduled(cron="* * 1 * * *")
public BatchStatus scheduleJob() {
final JobExecution ex = customJobLauncher.runJob(customJob, getJobParameters());
return ex.getStatus();
}
JobParameters getJobParameters() {
// Get the job parameters
}
}