Quartz集群-@DisallowConcurrentExecution避免跨实例并行运行



我有springBoot应用程序在多个实例中运行Quarts(2.3.0(,集群模式为true。

我已经配置了作业,并在每次运行之间提供了2秒的延迟。

@Configuration
public class SchedulerConfig {

@Bean
public JobDetail jobDetail() {
return JobBuilder.newJob()
.ofType(BatchTriggerJob.class)
.storeDurably()
.withIdentity("SCHEDULER")
.withDescription("event")
.build();
}
@Bean
public Trigger trigger() {
return TriggerBuilder
.newTrigger()
.forJob(jobDetail())
.withIdentity("BATCH")
.withDescription("SCHEDULER")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInSeconds(2)
.withMisfireHandlingInstructionIgnoreMisfires())
.build();
}
}

我已启用DisableConcurrentExecution

@DisallowConcurrentExecution
public class BatchTriggerJob extends QuartzJobBean {
@Autowired
private SchedulerService schedulerService;
@Override
protected void executeInternal(JobExecutionContext context) {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
schedulerService.processBatch(); //business logic and may take more than 2 sec
}
}

application.yml

quartz:
job-store-type: jdbc
jdbc:
initialize-schema: always
properties:
org:
quartz:
scheduler:
instanceName: EVENT_SCHEDULER
instanceId: AUTO
jobStore:
isClustered: true
misfireThreshold: 60000
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
tablePrefix: QRTZ_
threadPool:
threadCount: 1

但是,如果作业处理时间超过2秒,则另一个实例将开始执行作业。

我希望在启用集群模式的情况下在实例之间顺序执行,并且只有当当前作业在另一个实例中完成时,才在实例之间启动另一个作业。

PS:即使延迟<而不是处理时间。由于@DisableConcurrentExecution ,作业正在按顺序运行

以下是Quartz可能忽略@DisallowConcurrentExecution的一些可能原因

  • Quartz属性org.quartz.jobStore.isClustered未设置为true

    true不是Spring Boot中的默认值!您必须提供此石英属性。例如,通过application.properties:

    spring.quartz.properties.org.quartz.jobStore.isClustered=true
    
  • 如果通过标准Spring集成创建作业,则元数据将仅存储在QRTZ_job_DETAILS中一次。如果稍后添加注释,IS_NONCONCURRENT将不会更新。

最新更新