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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With