【问题标题】:How to cancel a Quartz job instead of enqueue in case of @DisallowConcurrentExecution在 @DisallowConcurrentExecution 的情况下如何取消 Quartz 作业而不是入队
【发布时间】:2016-10-05 21:49:15
【问题描述】:

Quartz 在@DisallowConcurrentExecution 的情况下,如果相同的作业(JobA)实例正在执行,则对作业(JobA)进行排队。但我想取消该作业而不是将其排队执行

【问题讨论】:

    标签: java quartz-scheduler


    【解决方案1】:

    我有一个使用 TriggerListeners 的解决方案,但使用此解决方案,您无法使用触发多个作业的触发器

    class CustomTriggerListener extends TriggerListenerSupport {
    
        private JobExecutionContext lastJobExecutionContext;
    
        @Override
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
            boolean vetoExecution = false;
            if (lastJobExecutionContext == null) {
                lastJobExecutionContext = context;
            } else {
                boolean lastJobIsDone = lastJobExecutionContext.getJobRunTime() >= 0;
    
                if (lastJobIsDone) {
                    lastJobExecutionContext = context;
                } else {
                    vetoExecution = true;
                }
            }
    
            return vetoExecution;
        }
    
        @Override
        public String getName() {
            return "CustomTriggerListener";
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      检查作业是否已经在运行,如果是则中断该作业

      for (int i = 0; i < scheduler.getCurrentlyExecutingJobs().size(); i++) {
          if(scheduler.getCurrentlyExecutingJobs().get(i).
                              getJobDetail().getKey().getName().equals(jobName)) {
                  scheduler.interrupt(new JobKey(jobName, "group1"));
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 2012-11-13
        • 2019-10-04
        相关资源
        最近更新 更多