Spring batch - running multiple jobs in parallelRun two jobs in parallel using spring batchMultiple jobs in spring batch not working with xml setupWhat is the difference between concurrency and parallelism?What's the difference between @Component, @Repository & @Service annotations in Spring?Recommended approach for parallel spring batch jobsSpring Batch Integration - Multiple files as single MessageSpring Integration poller starting same Spring Batch job multiple timesHow to run parallel jobs in spring batchSpring Batch Job Running in Infinite loopDynamically create Job in Spring Batchhow to trigger spring batch from spring integration java dsl Integration file poller integrationflowsSpring integration with spring batch

New Order #2: Turn My Way

Calculate Pi using Monte Carlo

Showing mass murder in a kid's book

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?

Trouble reading roman numeral notation with flats

Mortal danger in mid-grade literature

Why didn’t Eve recognize the little cockroach as a living organism?

What is it called when someone votes for an option that's not their first choice?

Output visual diagram of picture

Are hand made posters acceptable in Academia?

Can you take a "free object interaction" while incapacitated?

If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?

Is this saw blade faulty?

How to test the sharpness of a knife?

What is the meaning of "You've never met a graph you didn't like?"

Are all namekians brothers?

Relations between homogeneous polynomials

Derivative of an interpolated function

Why does the frost depth increase when the surface temperature warms up?

How do I prevent inappropriate ads from appearing in my game?

Checking @@ROWCOUNT failing

Why can't I get pgrep output right to variable on bash script?

Do people actually use the word "kaputt" in conversation?



Spring batch - running multiple jobs in parallel


Run two jobs in parallel using spring batchMultiple jobs in spring batch not working with xml setupWhat is the difference between concurrency and parallelism?What's the difference between @Component, @Repository & @Service annotations in Spring?Recommended approach for parallel spring batch jobsSpring Batch Integration - Multiple files as single MessageSpring Integration poller starting same Spring Batch job multiple timesHow to run parallel jobs in spring batchSpring Batch Job Running in Infinite loopDynamically create Job in Spring Batchhow to trigger spring batch from spring integration java dsl Integration file poller integrationflowsSpring integration with spring batch













2















I am new to Spring batch and couldn't figure out how to do this..



Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.



These files are somewhat similar, so the same processor/writer is used.



Right now the way I set up, every polling cycle 1 file is picked up and job is ran.



Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?










share|improve this question


























    2















    I am new to Spring batch and couldn't figure out how to do this..



    Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.



    These files are somewhat similar, so the same processor/writer is used.



    Right now the way I set up, every polling cycle 1 file is picked up and job is ran.



    Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?










    share|improve this question
























      2












      2








      2








      I am new to Spring batch and couldn't figure out how to do this..



      Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.



      These files are somewhat similar, so the same processor/writer is used.



      Right now the way I set up, every polling cycle 1 file is picked up and job is ran.



      Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?










      share|improve this question














      I am new to Spring batch and couldn't figure out how to do this..



      Basically I have a spring file poller which runs every N mins to look for files with some name (ex: A.txt & B.txt) in certain directory. At any moment in time, there could be max 2 files in this directory (A and B). Through Spring Batch Job, these two files will be processed and persisted to 2 different DB tables.



      These files are somewhat similar, so the same processor/writer is used.



      Right now the way I set up, every polling cycle 1 file is picked up and job is ran.



      Let's say there are 2 files in the directory (A.txt and B.txt), is there a way to create 2 jobs so that both jobs can be run in parallel?







      spring parallel-processing spring-integration spring-batch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 '17 at 16:38









      ljustinljustin

      2319




      2319






















          2 Answers
          2






          active

          oldest

          votes


















          3














          I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.



          Then you may start by configuring your own asynchronous JobLauncher. For example:



           @Bean
          public JobLauncher jobLauncher() throws Exception

          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

          jobLauncher.setJobRepository(jobRepository);
          jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
          jobLauncher.afterPropertiesSet();

          return jobLauncher;



          Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:



          Asynchronous



          Compare it with the synchronous execution flow:



          enter image description here



          Maybe it would additionally help to quote the SimpleJobLauncher java doc:




          Simple implementation of the JobLauncher interface. The Spring Core
          TaskExecutor interface is used to launch a Job. This means that the
          type of executor set is very important. If a SyncTaskExecutor is used,
          then the job will be processed within the same thread that called the
          launcher. Care should be taken to ensure any users of this class
          understand fully whether or not the implementation of TaskExecutor
          used will start tasks synchronously or asynchronously. The default
          setting uses a synchronous task executor.




          More details and configuration options - here.



          At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:



           @Autowired
          public JobBuilderFactory jobBuilderFactory;

          public Job createJobA()
          return jobBuilderFactory.get("A.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();


          public Job createJobB()
          return jobBuilderFactory.get("B.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();



          Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.






          share|improve this answer

























          • Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

            – Abhi
            Mar 12 at 6:40


















          6














          There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher. The JobLauncher has a taskExecutor property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.



          You can find all the TaskExecutor types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring



          For example SimpleAsyncTaskExecutor is a task executor that will create a new Thread on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors types that provides pooling features in order to reuse resources and maximize the efficiency of the system.



          Here is an small example of how configure a ThreadPoolTaskExecutor:



          A) Configure ThreadPoolTaskExecutor Bean



          @Bean
          public ThreadPoolTaskExecutor taskExecutor()
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setCorePoolSize(15);
          taskExecutor.setMaxPoolSize(20);
          taskExecutor.setQueueCapacity(30);
          return taskExecutor;



          B) Configure JobLauncher Bean



           @Bean
          public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          jobLauncher.setTaskExecutor(taskExecutor);
          jobLauncher.setJobRepository(jobRepository);
          return jobLauncher;



          C) Inject your JobLauncher and your Jobs configuration



          @Autowired
          private JobLauncher jobLauncher;

          @Autowired
          @Qualifier("job1-file-A")
          private Job job1;

          @Autowired
          @Qualifier("job2-file-B")
          private Job job2;


          D) Schedule the jobs



          @Scheduled(cron = "*/1 * * * * *")
          public void run1()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job1, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());




          @Scheduled(cron = "*/1 * * * * *")
          public void run2()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job2, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());





          E) Finally on your SpringBoot Class @EnableBatchProcessing and @EnableScheduling



          @EnableBatchProcessing
          @EnableScheduling
          @SpringBootApplication
          public class MyBatchApp {





          share|improve this answer























          • It should be the accepted answer.

            – davidxxx
            Mar 7 at 20:40










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718888%2fspring-batch-running-multiple-jobs-in-parallel%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.



          Then you may start by configuring your own asynchronous JobLauncher. For example:



           @Bean
          public JobLauncher jobLauncher() throws Exception

          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

          jobLauncher.setJobRepository(jobRepository);
          jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
          jobLauncher.afterPropertiesSet();

          return jobLauncher;



          Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:



          Asynchronous



          Compare it with the synchronous execution flow:



          enter image description here



          Maybe it would additionally help to quote the SimpleJobLauncher java doc:




          Simple implementation of the JobLauncher interface. The Spring Core
          TaskExecutor interface is used to launch a Job. This means that the
          type of executor set is very important. If a SyncTaskExecutor is used,
          then the job will be processed within the same thread that called the
          launcher. Care should be taken to ensure any users of this class
          understand fully whether or not the implementation of TaskExecutor
          used will start tasks synchronously or asynchronously. The default
          setting uses a synchronous task executor.




          More details and configuration options - here.



          At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:



           @Autowired
          public JobBuilderFactory jobBuilderFactory;

          public Job createJobA()
          return jobBuilderFactory.get("A.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();


          public Job createJobB()
          return jobBuilderFactory.get("B.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();



          Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.






          share|improve this answer

























          • Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

            – Abhi
            Mar 12 at 6:40















          3














          I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.



          Then you may start by configuring your own asynchronous JobLauncher. For example:



           @Bean
          public JobLauncher jobLauncher() throws Exception

          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

          jobLauncher.setJobRepository(jobRepository);
          jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
          jobLauncher.afterPropertiesSet();

          return jobLauncher;



          Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:



          Asynchronous



          Compare it with the synchronous execution flow:



          enter image description here



          Maybe it would additionally help to quote the SimpleJobLauncher java doc:




          Simple implementation of the JobLauncher interface. The Spring Core
          TaskExecutor interface is used to launch a Job. This means that the
          type of executor set is very important. If a SyncTaskExecutor is used,
          then the job will be processed within the same thread that called the
          launcher. Care should be taken to ensure any users of this class
          understand fully whether or not the implementation of TaskExecutor
          used will start tasks synchronously or asynchronously. The default
          setting uses a synchronous task executor.




          More details and configuration options - here.



          At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:



           @Autowired
          public JobBuilderFactory jobBuilderFactory;

          public Job createJobA()
          return jobBuilderFactory.get("A.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();


          public Job createJobB()
          return jobBuilderFactory.get("B.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();



          Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.






          share|improve this answer

























          • Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

            – Abhi
            Mar 12 at 6:40













          3












          3








          3







          I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.



          Then you may start by configuring your own asynchronous JobLauncher. For example:



           @Bean
          public JobLauncher jobLauncher() throws Exception

          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

          jobLauncher.setJobRepository(jobRepository);
          jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
          jobLauncher.afterPropertiesSet();

          return jobLauncher;



          Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:



          Asynchronous



          Compare it with the synchronous execution flow:



          enter image description here



          Maybe it would additionally help to quote the SimpleJobLauncher java doc:




          Simple implementation of the JobLauncher interface. The Spring Core
          TaskExecutor interface is used to launch a Job. This means that the
          type of executor set is very important. If a SyncTaskExecutor is used,
          then the job will be processed within the same thread that called the
          launcher. Care should be taken to ensure any users of this class
          understand fully whether or not the implementation of TaskExecutor
          used will start tasks synchronously or asynchronously. The default
          setting uses a synchronous task executor.




          More details and configuration options - here.



          At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:



           @Autowired
          public JobBuilderFactory jobBuilderFactory;

          public Job createJobA()
          return jobBuilderFactory.get("A.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();


          public Job createJobB()
          return jobBuilderFactory.get("B.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();



          Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.






          share|improve this answer















          I believe that you can. Since you are new in spring batch (just like me) I would recommend that you go through the domain language of a batch if you haven't done so already.



          Then you may start by configuring your own asynchronous JobLauncher. For example:



           @Bean
          public JobLauncher jobLauncher() throws Exception

          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

          jobLauncher.setJobRepository(jobRepository);
          jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
          jobLauncher.afterPropertiesSet();

          return jobLauncher;



          Pay special attention to SimpleAsyncTaskExecutor (the job repo can be autowired). This configuration will allow asynchronous execution as visualized next:



          Asynchronous



          Compare it with the synchronous execution flow:



          enter image description here



          Maybe it would additionally help to quote the SimpleJobLauncher java doc:




          Simple implementation of the JobLauncher interface. The Spring Core
          TaskExecutor interface is used to launch a Job. This means that the
          type of executor set is very important. If a SyncTaskExecutor is used,
          then the job will be processed within the same thread that called the
          launcher. Care should be taken to ensure any users of this class
          understand fully whether or not the implementation of TaskExecutor
          used will start tasks synchronously or asynchronously. The default
          setting uses a synchronous task executor.




          More details and configuration options - here.



          At the end just create the jobs with different names and/or launch them with different parameter set. Naive example would be:



           @Autowired
          public JobBuilderFactory jobBuilderFactory;

          public Job createJobA()
          return jobBuilderFactory.get("A.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();


          public Job createJobB()
          return jobBuilderFactory.get("B.txt")
          .incrementer(new RunIdIncrementer())
          .flow(step1())
          .next(step2())
          .end()
          .build();



          Executing these jobs with your asynchronous job launcher will create two job instances that which will execute in parallel. This is just one option, that may or may not be suitable for your context.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 16 '17 at 19:29

























          answered Aug 16 '17 at 18:52









          Lachezar BalevLachezar Balev

          7,02843052




          7,02843052












          • Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

            – Abhi
            Mar 12 at 6:40

















          • Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

            – Abhi
            Mar 12 at 6:40
















          Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

          – Abhi
          Mar 12 at 6:40





          Awesome example. It would also be highly beneficial if you could list the method on how to capture the completion of the job in the main thread. Just to complete the great answer! :)

          – Abhi
          Mar 12 at 6:40













          6














          There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher. The JobLauncher has a taskExecutor property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.



          You can find all the TaskExecutor types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring



          For example SimpleAsyncTaskExecutor is a task executor that will create a new Thread on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors types that provides pooling features in order to reuse resources and maximize the efficiency of the system.



          Here is an small example of how configure a ThreadPoolTaskExecutor:



          A) Configure ThreadPoolTaskExecutor Bean



          @Bean
          public ThreadPoolTaskExecutor taskExecutor()
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setCorePoolSize(15);
          taskExecutor.setMaxPoolSize(20);
          taskExecutor.setQueueCapacity(30);
          return taskExecutor;



          B) Configure JobLauncher Bean



           @Bean
          public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          jobLauncher.setTaskExecutor(taskExecutor);
          jobLauncher.setJobRepository(jobRepository);
          return jobLauncher;



          C) Inject your JobLauncher and your Jobs configuration



          @Autowired
          private JobLauncher jobLauncher;

          @Autowired
          @Qualifier("job1-file-A")
          private Job job1;

          @Autowired
          @Qualifier("job2-file-B")
          private Job job2;


          D) Schedule the jobs



          @Scheduled(cron = "*/1 * * * * *")
          public void run1()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job1, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());




          @Scheduled(cron = "*/1 * * * * *")
          public void run2()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job2, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());





          E) Finally on your SpringBoot Class @EnableBatchProcessing and @EnableScheduling



          @EnableBatchProcessing
          @EnableScheduling
          @SpringBootApplication
          public class MyBatchApp {





          share|improve this answer























          • It should be the accepted answer.

            – davidxxx
            Mar 7 at 20:40















          6














          There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher. The JobLauncher has a taskExecutor property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.



          You can find all the TaskExecutor types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring



          For example SimpleAsyncTaskExecutor is a task executor that will create a new Thread on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors types that provides pooling features in order to reuse resources and maximize the efficiency of the system.



          Here is an small example of how configure a ThreadPoolTaskExecutor:



          A) Configure ThreadPoolTaskExecutor Bean



          @Bean
          public ThreadPoolTaskExecutor taskExecutor()
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setCorePoolSize(15);
          taskExecutor.setMaxPoolSize(20);
          taskExecutor.setQueueCapacity(30);
          return taskExecutor;



          B) Configure JobLauncher Bean



           @Bean
          public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          jobLauncher.setTaskExecutor(taskExecutor);
          jobLauncher.setJobRepository(jobRepository);
          return jobLauncher;



          C) Inject your JobLauncher and your Jobs configuration



          @Autowired
          private JobLauncher jobLauncher;

          @Autowired
          @Qualifier("job1-file-A")
          private Job job1;

          @Autowired
          @Qualifier("job2-file-B")
          private Job job2;


          D) Schedule the jobs



          @Scheduled(cron = "*/1 * * * * *")
          public void run1()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job1, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());




          @Scheduled(cron = "*/1 * * * * *")
          public void run2()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job2, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());





          E) Finally on your SpringBoot Class @EnableBatchProcessing and @EnableScheduling



          @EnableBatchProcessing
          @EnableScheduling
          @SpringBootApplication
          public class MyBatchApp {





          share|improve this answer























          • It should be the accepted answer.

            – davidxxx
            Mar 7 at 20:40













          6












          6








          6







          There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher. The JobLauncher has a taskExecutor property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.



          You can find all the TaskExecutor types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring



          For example SimpleAsyncTaskExecutor is a task executor that will create a new Thread on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors types that provides pooling features in order to reuse resources and maximize the efficiency of the system.



          Here is an small example of how configure a ThreadPoolTaskExecutor:



          A) Configure ThreadPoolTaskExecutor Bean



          @Bean
          public ThreadPoolTaskExecutor taskExecutor()
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setCorePoolSize(15);
          taskExecutor.setMaxPoolSize(20);
          taskExecutor.setQueueCapacity(30);
          return taskExecutor;



          B) Configure JobLauncher Bean



           @Bean
          public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          jobLauncher.setTaskExecutor(taskExecutor);
          jobLauncher.setJobRepository(jobRepository);
          return jobLauncher;



          C) Inject your JobLauncher and your Jobs configuration



          @Autowired
          private JobLauncher jobLauncher;

          @Autowired
          @Qualifier("job1-file-A")
          private Job job1;

          @Autowired
          @Qualifier("job2-file-B")
          private Job job2;


          D) Schedule the jobs



          @Scheduled(cron = "*/1 * * * * *")
          public void run1()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job1, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());




          @Scheduled(cron = "*/1 * * * * *")
          public void run2()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job2, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());





          E) Finally on your SpringBoot Class @EnableBatchProcessing and @EnableScheduling



          @EnableBatchProcessing
          @EnableScheduling
          @SpringBootApplication
          public class MyBatchApp {





          share|improve this answer













          There are very good approaches in order to run jobs in async mode with Spring, it is just a matter of how is configured the JobLauncher. The JobLauncher has a taskExecutor property and the asynchronous execution could be activated depending on the implementation that is assigned to that property.



          You can find all the TaskExecutor types that Spring can provide and depending on your needs select the best approach to accomplish your batch asynchronous jobs. Task Executors Types in Spring



          For example SimpleAsyncTaskExecutor is a task executor that will create a new Thread on any invocation and that could generate a performance issue if the execution runs with high frequency. In the other hand there are also TaskExecutors types that provides pooling features in order to reuse resources and maximize the efficiency of the system.



          Here is an small example of how configure a ThreadPoolTaskExecutor:



          A) Configure ThreadPoolTaskExecutor Bean



          @Bean
          public ThreadPoolTaskExecutor taskExecutor()
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setCorePoolSize(15);
          taskExecutor.setMaxPoolSize(20);
          taskExecutor.setQueueCapacity(30);
          return taskExecutor;



          B) Configure JobLauncher Bean



           @Bean
          public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository)
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          jobLauncher.setTaskExecutor(taskExecutor);
          jobLauncher.setJobRepository(jobRepository);
          return jobLauncher;



          C) Inject your JobLauncher and your Jobs configuration



          @Autowired
          private JobLauncher jobLauncher;

          @Autowired
          @Qualifier("job1-file-A")
          private Job job1;

          @Autowired
          @Qualifier("job2-file-B")
          private Job job2;


          D) Schedule the jobs



          @Scheduled(cron = "*/1 * * * * *")
          public void run1()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job1, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());




          @Scheduled(cron = "*/1 * * * * *")
          public void run2()
          Map<String, JobParameter> confMap = new HashMap<>();
          confMap.put("time", new JobParameter(System.currentTimeMillis()));
          JobParameters jobParameters = new JobParameters(confMap);
          try
          jobLauncher.run(job2, jobParameters);
          catch (Exception ex)
          logger.error(ex.getMessage());





          E) Finally on your SpringBoot Class @EnableBatchProcessing and @EnableScheduling



          @EnableBatchProcessing
          @EnableScheduling
          @SpringBootApplication
          public class MyBatchApp {






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 17 '17 at 2:45









          Daniel C.Daniel C.

          2,81721019




          2,81721019












          • It should be the accepted answer.

            – davidxxx
            Mar 7 at 20:40

















          • It should be the accepted answer.

            – davidxxx
            Mar 7 at 20:40
















          It should be the accepted answer.

          – davidxxx
          Mar 7 at 20:40





          It should be the accepted answer.

          – davidxxx
          Mar 7 at 20:40

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718888%2fspring-batch-running-multiple-jobs-in-parallel%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme