如何在石英调度程序中停止特定的作业

时间:2016-09-27 20:19:22

标签: java scheduled-tasks quartz-scheduler

如何在石英调度程序中停止特定的作业我已经阅读了多个类似的问题但是大多数问题都没有答案,而且有一个问题已经过时并且引用了不再存在的文档

大多数问题的回答是You need to write a your job as an implementation of InterruptableJob. To interrupt this job, you need handle to Scheduler, and call interrupt(jobKey<<job name & job group>>)以及此死链接点http://www.quartz-scheduler.org/api/2.0.0/org/quartz/InterruptableJob.html

但是有没有人有一个如何做到这一点的例子

1 个答案:

答案 0 :(得分:1)

您可以在此处找到解释:http://forums.terracotta.org/forums/posts/list/7700.page

相关部分是:

 public void interrupt() throws UnableToInterruptJobException
 {
     stopFlag.set(true);
     Thread thread = workerThread.getAndSet(null);
     if (thread != null)
         thread.interrupt();
 }

你可以这样称呼它:

 SchedulerFactory schedulerFactory = new StdSchedulerFactory();
 Scheduler scheduler = schedulerFactory.getScheduler();
 List<JobExecutionContext> currentlyExecuting = scheduler.getCurrentlyExecutingJobs();

 for( JobExecutionContext jobExecutionContext : currentlyExecuting)
 {
      if( jobExecutionContext.getJobDetail().getKey().getName().equals( "Name"))
      {
            scheduler.interrupt( jobExecutionContext.getJobDetail().getKey());
      }
 }