Quartz 阻止在 jobToBeExecuted 上执行作业

Quartz prevent job execution on jobToBeExecuted

我的目标是创建一个队列系统,我可以在其中为每个组指定最大并发作业数,即对于组 A,最多 3 个作业应该同时 运行,对于组 B 最大 Y作业等。作业可以按 cron 计划执行,也可以使用 SimpleTrigger 执行一次,因此在安排作业时我无法检查队列,我必须在执行之前或期间检查它。我正在实施一个 joblistener,我试图阻止在 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
}

}

你能看看吗TriggerListener

您应该实施 TriggerListener 并在 "vetoJobExecution" 方法中包含您的中止逻辑。

boolean vetoJobExecution(Trigger trigger,
                     JobExecutionContext context)

Its Called by the Scheduler when a Trigger has fired, and it's associated JobDetail is about to be executed. If the implementation vetos the execution (via returning true), the job's execute method will not be called.