Friday, 1 December 2017

Spring Batch Job Execution Mode

Problem: When a spring batch has multiple jobs implemented, it is expected to run in parallel within the JVM instance. I noticed that the jobs are executed sequentially, not in parallel. In my case, we had 8 jobs with the project, irrespective of the cron schedule, the jobs are executed in sequential mode.

Root Cause: I was using JobLauncher instance (@Autowired instance-Singleton) every time while initiating jobs using @Scheduled annotation. It makes the execution of jobs in a sequential fashion.
Singleton JobLauncher makes the execution of multiple jobs sequential.

Solution: I’ve modified the code to create SimpleJobLauncher instance to initiate the job everytime, so that the execution of jobs were made parallel.



Also,
Use ThreadPoolTaskExecutor, while building a Step for a Job. For example, refer the below snapshot of code. During execution, 2 threads will be created for processing the Reader data. Based on the chunk size, the reader data will be processed by 2 threads in parallel.
1,2,3 will be processed by Thread 1
4,5,6 will be processed by Thread 2
7,8 will be processed by Thread 1




If you want to initiate specific job instance as standalone, refer the below code snippet. Read the beanName from Program Argument and invoke the method with cronPattern. If there are multiple jobs, then you can specify the beanName and start jvm instance. So that you will have control over the job execution.







Recent Posts

Micro VMs & Unikernels

This post is a follow up of this post . In Previous Post, we discussed about Virtual Machines & Containers architecture. In this post, w...

Older Posts