29 Eylül 2023 Cuma

SpringQuartz @DisallowConcurrentExecution Anotasyonu

Giriş
Şu satırı dahil ederiz
import  org.quartz.DisallowConcurrentExecution;
Örnek
Şöyle yaparız
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

@DisallowConcurrentExecution
public class MyQuartzJob implements Job {
  @Override
  public void execute(JobExecutionContext context) 
  throws JobExecutionException {
    // Your job logic here
  }
}
Açıklaması şöyle
Quartz provides built-in support for locking jobs to prevent concurrent executions. You can set the @DisallowConcurrentExecution annotation on your job classes to ensure that only one instance of a job runs at a time

By adding @DisallowConcurrentExecution, Quartz will automatically ensure that a job instance is locked while it's running, preventing concurrent executions of the same job.


Örnek
Açıklaması şöyle
In the SampleCronJob I have used a annotation @DisallowConcurrentExecution once this is added to a job and if we have multiple scheduler instance are running concurrently this job will not be executed by multiple schedulers concurrently.
Şöyle yaparız
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

@DisallowConcurrentExecution
public class SampleCronJob extends QuartzJobBean {
  @Override
  protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
    ...
  }
}

Hiç yorum yok:

Yorum Gönder