Spring batch restrict single instance of job only

11,696

Solution 1

You could try to intercept the job execution, implementing the JobExecutionListener interface:

public class MyJobExecutionListener extends JobExecutionListener {
    //active JobExecution, used as a lock.
    private JobExecution _active;
    public void beforeJob(JobExecution jobExecution) {
        //create a lock
        synchronized(jobExecution) {
            if(_active!=null && _active.isRunning()) {
                jobExecution.stop();
            } else {
                _active=jobExecution;
            }
        }
    }
    public void afterJob(JobExecution jobExecution) {
          //release the lock
          synchronized(jobExecution) {
              if(jobExecution==_active) {
                _active=null; 
              }
          }
    }
}

And then, inject to the Job definition:

<job id="myJobConfig">
    <listeners>
        <listener ref="myListener"/>
    </listeners>
</job>

Solution 2

I solved this by creating an JobExecutionListner and with the help of JobExplorer I checked if any other instance is running if running then stop current job.I created listener so that it can be plugged in to any job that requires this kind of scenario.

Set<JobExecution> jobExecutions = ((SimpleJobExplorer) jobExplorer.getObject()).findRunningJobExecutions(jobExecution.getJobInstance().getJobName());
            if(jobExecutions.size()>1){
                Long currentTime = (new Date()).getTime();
                for(JobExecution execution : jobExecutions ){
                    if(execution.getJobInstance().getId().compareTo(jobExecution.getJobInstance().getId())!=0 && (currentTime - execution.getStartTime().getTime()) <lockOverideTime){
                        jobExecution.stop();
                        throw new IllegalStateException("Another instance of the job running job name : " +jobExecution.getJobInstance().getJobName() );
                    }
                }
            }

Solution 3

Or, in response to REST URL, check using JobExplorer if your job is running using job's specifics business rules

Share:
11,696

Related videos on Youtube

Author by

sandeep

Updated on October 22, 2022

Comments

  • sandeep less than a minute

    I have one spring batch job which can be kicked of by rest URL. I want to make sure only one job instance is allowed to run. and if another instance already running then don't start another. even if the parameters are different.

    I searched and found nothing out of box solution. thinking of extending SimpleJobLauncher. to check if any instance of the job running or not.

  • sandeep over 8 years
    Thanks for this solution I need to try this and will update if it works for me and meet my all requirements.
  • kukudas
    kukudas almost 6 years
    What happens if you shut down the server while a job is running? If you restart the server it will be still marked as started but is actually broken. Wouldn't this solution then not start an instance of this job anymore?
  • Ivo van der Veeken
    Ivo van der Veeken almost 4 years
    findRunningJobExecutions() basically returns job executions without an endTime. If you have older executions that didn't finish correctly, this won't work.
  • Harshit 10 months
    You are a lifesaver, Tomas. Thanks a lot. I used java config and it worked like a charm jobBuilderFactory.get("jobName").listener(new BatchJobExecutionListener()).start...