如何手动停止/中断石英调度程序作业

我在主类中运行一个简单的石英作业,每30秒运行一次。

public class Start { public static void main(String[] args) throws Exception { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); JobDetail job = newJob(MyJob.class).withIdentity("myJob","XXX").build(); Trigger trigger = TriggerBuilder.newTrigger() .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(30) .repeatForever()) .build(); sched.scheduleJob(job, trigger); sched.start(); } } 

在这里,我正在实施InterruptableJob

  public class MyJob implements InterruptableJob { private volatile boolean isJobInterrupted = false; private JobKey jobKey = null; private volatile Thread thisThread; public MyJob() { } @Override public void interrupt() throws UnableToInterruptJobException { // TODO Auto-generated method stub System.err.println("calling interrupt:"+thisThread+"==>"+jobKey); isJobInterrupted = true; if (thisThread != null) { // this call causes the ClosedByInterruptException to happen thisThread.interrupt(); } } @Override public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub thisThread = Thread.currentThread(); jobKey = context.getJobDetail().getKey(); System.err.println("calling execute:"+thisThread+"==>"+jobKey); } } 

现在我试图使用另一个主要课程来阻止这项工作,就像一切都没有运气一样

 public class Stop { public static void main(String[] args) throws Exception { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); // get a "nice round" time a few seconds in the future... Date startTime = nextGivenSecondDate(null, 1); JobDetail job = newJob(MyJob.class).withIdentity("myJob", "XXX").build(); Trigger trigger = TriggerBuilder.newTrigger() .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(30) .repeatForever()) .build(); sched.scheduleJob(job, trigger); sched.start(); try { // if you want to see the job to finish successfully, sleep for about 40 seconds Thread.sleep(60000) ; // tell the scheduler to interrupt our job sched.interrupt(job.getKey()); Thread.sleep(3 * 1000L); } catch (Exception e) { e.printStackTrace(); } System.err.println("------- Shutting Down --------"); TriggerKey tk=TriggerKey.triggerKey("myJob","group1"); System.err.println("tk"+tk+":"+job.getKey()); sched.unscheduleJob(tk); sched.interrupt(job.getKey()); sched.interrupt("myJob"); sched.deleteJob(job.getKey()); sched.shutdown(); System.err.println("------- Shutting Down "); sched.shutdown(false); System.err.println("------- Shutdown Complete "); System.err.println("------- Shutdown Complete "); } } 

任何人都可以告诉我正确的方法来阻止这项工作吗? 非常感谢。

这个问题似乎回答了你所描述的确切问题:

你需要写一份你的工作作为InterruptableJob的实现。 要中断此作业,您需要处理Scheduler ,并调用interrupt(jobKey<>)

根据InterruptableJob 文档 :

由Jobs实现的接口,提供执行中断的机制。 实现这个界面不需要工作 – 事实上,对于大多数人来说,他们的工作都不会。

中断作业在概念和挑战中非常类似于 Java中线程的正常中断。

实际中断作业的方法必须在作业本身内实现(此接口的interrupt()方法只是调度程序通知作业已请求中断的一种方法)。 您的作业用于中断自身的机制可能因实现而异。 但是,任何实现中的原则思想应该是让作业的主体执行(..)定期检查一些标志以查看是否已请求中断,如果设置了标志,则以某种方式中止其余部分的性能。工作的工作。

强调我的。 它类似但不一样。 你不应该使用Threads(但实际上,如果那就是你的Job所做的那样……)。

可以在类org.quartz.examples.DumbInterruptableJob的java源代码中找到中断作业的示例。 在interrupt()和execute(..)中使用wait()和notify()同步的某种组合是合法的,以便使interrupt()方法阻塞,直到执行(..)信号表明它已经注意到该集合旗。

因此,我建议您阅读 完整下载中 的文档并检查示例。