Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Batch: How to ensure when a Job is running, it is not allowed to run again at the same time

How to ensure when a Job is running, it is not allowed to run again at the same time?

We have BJ that takes 1 hour to process the feed and populate the temp tables. First step of this BJ is to clear the temp tables and start populating the data from the Main store front tables.

Consider a scenario that when the BJ is started (first time running), if we start the BJ again, it will delete content from temp tables as part of Step one.

So please suggest on how i can hold the second execution till the first is not COMPLETED?

like image 644
techanuva Avatar asked Mar 27 '13 07:03

techanuva


2 Answers

You may create custom Tasklet as your first step and use JobExecutionDao in it to find all JobExecutions. If there are more than one running - throw an exception.

like image 111
Michail Nikolaev Avatar answered Sep 20 '22 22:09

Michail Nikolaev


I am sure, this will not be the best solution, but I hope this will serve your situation nevertheless.

While executing the job, make sure, you run the job with always the same parameter. After the completion of the successful execution of your job, configure your calling-scripts to delete the entry corresponding to that execution of the batch-job.

This way, it will give error and won't allow you to run 2 executions of the same job at the same time. Deletion will ensure the serial execution is allowed.

ALTERNATIVE METHOD: Write your job with a single parameter job-execution-id. Everytime before you execute the job, query the maximum value of job-execution-id for the completed jobs from the batch tables for the job. Now, execute the job with job-execution-id incremented by 1 as input parameter.

I think this is a better method than above. I am not sure, if springbatch itself provides any easy-take-away options for implementing this scenario.

like image 22
coder91 Avatar answered Sep 19 '22 22:09

coder91