Multiple spring batch jobs

这一生的挚爱 提交于 2019-12-02 13:28:39

问题


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:


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;
 }


来源:https://stackoverflow.com/questions/30332722/multiple-spring-batch-jobs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!