1
votes

I am using the @Scheduled annotation in Spring Boot to trigger multiple jobs.

Following is the code snippet:

    @EnableBatchProcessing
    @EnableScheduling
    public class Config extends DefaultBatchConfigurer{

            @Autowired
            JobLauncher launcher;

            @Scheduled
            public void run(){
            String[] argList = {"A", "B"};
            for(String char : argList){
                launcher.run(job(), 
                new JobParametersBuilder().
                addString("char", char).
                toJobParameters());
               } 
             }

       public Job job(){
       //Job definition is here.
       }
    }

However, this triggers the 2 jobs in succession and not in parallel, i.e. the application waits for job with parameter "A" to complete before starting job with paramater "B".

Is there a way I can run these jobs in parallel?

Thanks!

1
Are you using the default JobLauncher? If so, it's synchronous by default. - Michael Minella

1 Answers

3
votes

You can define your JobLauncher in some config file and set SimpleAsyncTaskExecutor as task executor which will run jobs async (not waiting for one to complete in order to start next one).

Here is code snippet from :

@Bean
public JobLauncher jobLauncher() {
    final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
    jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
    return jobLauncher;
 }