【问题标题】:Quartz prevent job execution on jobToBeExecutedQuartz 阻止在 jobToBeExecuted 上执行作业
【发布时间】:2017-04-20 20:17:42
【问题描述】:

我的目标是创建一个队列系统,我可以在其中为每个组指定最大数量的并发作业,即对于组 A 最多 3 个作业应该同时运行,对于组 B 最多 Y 个作业等。这些作业可以既可以在 cron 计划上执行,也只能使用 SimpleTrigger 执行一次,因此在调度作业时我无法检查队列,我必须在执行之前或执行期间检查它。我正在实现一个作业监听器,并试图阻止在 jobToBeExecuted() 方法中执行。我试过 scheduler.interrupt() 但是当工作还没有开始时它不起作用。 scheduler.deletejob() 和 scheduler.unschedule() 也没有阻止它执行。

有什么想法吗?

public class JobQueueListener implements JobListener {

@Override
public void jobToBeExecuted(JobExecutionContext context) {
     JobKey currentJobKey = context.getJobDetail().getKey();
     JobDetail jobDetail = context.getJobDetail();
     Scheduler scheduler = context.getScheduler();

     if (shouldBePutInQueue(currentJobKey)) {
          /// Prevent execution and put in queue here, but how?
     }
}

@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
       //Check queue and execute next in queue
}

}

【问题讨论】:

    标签: java quartz-scheduler quartz


    【解决方案1】:

    你能看看TriggerListener

    您应该实现 TriggerListener 并在“vetoJobExecution”方法中包含您的中止逻辑。

    boolean vetoJobExecution(Trigger trigger,
                         JobExecutionContext context)
    

    当触发器触发时由调度程序调用,并且它关联的 JobDetail 即将被执行。如果实现否决执行(通过返回 true),则不会调用作业的 execute 方法。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    相关资源
    最近更新 更多