Spring batch restrict single instance of job only

后端 未结 4 1438
遥遥无期
遥遥无期 2020-12-17 23:34

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

4条回答
  •  伪装坚强ぢ
    2020-12-18 00:10

    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:

    
        
            
        
    
    

提交回复
热议问题