I have multiple scheduled jobs in BatchScheduler which runs at a particular time. Simple inbuilt JobLauncher which is sync. in nature is used initially. Now, I want to run the jobs in parallel so that no job can wait for other to finish.
I have tried with the @Async annotation on my different jobs but it did not worked.
I have also tried creating different JobLauncher object for each and every job but it also did not worked.
Then, I tried with setting the jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()). But it did not worked.
I have also tried @Bean
public JobLauncher jobLauncher() {
final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
return jobLauncher;
}
I have tried all the combinations given in different stackoverflow answers but it did not worked.
@Bean
public JobLauncher jobLauncher() {
final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
return jobLauncher;
}
Actual: But it also did not worked.
As when I am checking the starttime for the batch jobs in batch tables. The job are starting when 1 job is finished.
Expected: Jobs should run in parallel.
SimpleJobLauncher
with an asynchronous task executor is the way to go (TheThreadPoolTaskExecutor
as mentioned by @OhadR is a good option). This will make jobs run in parallel using different threads from the pool. – Mahmoud Ben Hassine