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

编程入门 行业动态 更新时间:2024-10-11 03:16:36
本文介绍了如何手动停止/中断石英调度程序作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am running a simple quartz job in main class which runs every 30 secs.

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,如

Here i am implementing InterruptableJob like

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); } }

现在我试图用另一个主类停止工作喜欢以任何可能的方式没有运气

Now i tried to stop the job using another main class like in every possible way with no luck

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 "); } }

任何人都可以告诉我正确的停止工作的方法?非常感谢。

Can anyone please tell me the correct way to stop the job? Thanks a lot.

推荐答案

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

This question seems to answer the exact problem you're describing:

你需要写一份你的工作作为InterruptableJob的实现。要中断此作业,您需要处理计划程序,并调用中断(jobKey<<作业名称&作业组>>)

按 InterruptableJob 文档:

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

The interface to be implemented by Jobs that provide a mechanism for having their execution interrupted. It is NOT a requirement for jobs to implement this interface - in fact, for most people, none of their jobs will.

中断工作非常类似概念和对Java中线程正常中断的挑战。

Interrupting a Job is very analogous in concept and challenge to normal interruption of a Thread in Java.

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

The means of actually interrupting the Job must be implemented within the Job itself (the interrupt() method of this interface is simply a means for the scheduler to inform the Job that a request has been made for it to be interrupted). The mechanism that your jobs use to interrupt themselves might vary between implementations. However the principle idea in any implementation should be to have the body of the job's execute(..) periodically check some flag to see if an interruption has been requested, and if the flag is set, somehow abort the performance of the rest of the job's work.

强调我的。它类似但不一样。你不应该使用Threads(但事实上,如果那是你的 Job 那么......)。

Emphasis mine. It is analogous but not the same. You're not expected to use Threads (but indeed you could if that's what your Job does...).

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

An example of interrupting a job can be found in the java source for the class org.quartz.examples.DumbInterruptableJob. It is legal to use some combination of wait() and notify() synchronization within interrupt() and execute(..) in order to have the interrupt() method block until the execute(..) signals that it has noticed the set flag.

所以我推荐阅读文档并检查中的示例完整下载。

更多推荐

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

本文发布于:2023-11-25 20:51:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   程序   石英

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!