Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch - Executing multiple instances of a job at same time

Tags:

spring-batch

I have a clarification.

Is it possible for us to run multiple instances of a job at the same time.

Currently, we have single instance of a job at any given time.

If it is possible, please let me know how to do it.

like image 950
Anand Kumar Avatar asked Jun 08 '12 12:06

Anand Kumar


People also ask

How does multithreading work in Spring Batch?

Multithreaded steps. By default, Spring Batch uses the same thread to execute a batch job from start to finish, meaning that everything runs sequentially. Spring Batch also allows multithreading at the step level. This makes it possible to process chunks using several threads.


2 Answers

Yes you can. Spring Batch distinguishes jobs based on the JobParameters. So if you always pass different JobParameters to the same job, you will have multiple instances of the same job running. A simple way is just to add a UUID parameter to each request to start a job. Example:

final JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
jobParametersBuilder.addString("instance_id", UUID.randomUUID().toString(), true);
jobLauncher.run(job,jobParametersBuilder.toJobParameters());

The boolean 'true' at the end signal to Spring Batch to use that parameter as part of the 'identity' of the instance of the job, so you will always get new instances with each 'run' of the job.

like image 101
Antonio Dias Avatar answered Oct 15 '22 13:10

Antonio Dias


Yes you can very much run tasks in parallel as also documented here

But there are certain things to be considered

  • Does your application logic needs parallel execution? Because if if you are going to run steps in parallel, you would have to take care and build application logic so that the work done by parallel steps is not overlapping (Unless that is the intention of your application)
like image 27
Vishal Biyani Avatar answered Oct 15 '22 11:10

Vishal Biyani