背景
Spring Batch默认的JobLauncher执行Job的方式是同步的。
如果需要执行异步Job,则需要定义能执行异步Job的JobLauncher。
解决办法
在Configuration Class中定义SimpleJobLauncher, 设置其TaskExecutor为异步的SimpleAsyncTaskExecutor
1
2
3
4
5
6
7
8
9
10
11
public class BatchConfiguration {
public SimpleJobLauncher simpleJobLauncher(JobRepository jobRepository){
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
return jobLauncher;
}
}在调用Launcher的主程序中设置自动注入,就可以使用了。
1
2
3
4
5
6
7
8
9
10
11
12
13
SimpleJobLauncher simpleJobLauncher;
Job job1;
Job job2;
public void run(ApplicationArguments args) throws Exception {
logger.info("##### run async jobs #####");
simpleJobLauncher.run(job1, jobParameters);
simpleJobLauncher.run(job2, jobParameters);
}