小编Ket*_*tan的帖子

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();
  }

}
Run Code Online (Sandbox Code Playgroud)

我已启用 DisallowConcurrentExecution

@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
  }

}
Run Code Online (Sandbox Code Playgroud)

应用程序.yml

quartz:
    job-store-type: …
Run Code Online (Sandbox Code Playgroud)

java quartz-scheduler spring-scheduled spring-boot

5
推荐指数
1
解决办法
2682
查看次数