我正在主类中运行一个简单的石英作业,该作业每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();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我正在实现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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我试图用另一种主要的职业来停止工作,就像在没有运气的情况下
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 ");
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我停止工作的正确方法?非常感谢。
这个问题似乎可以回答您所描述的确切问题:
您需要编写一份工作作为InterruptableJob的实现。要中断此工作,您需要使用的句柄
Scheduler,然后调用interrupt(jobKey<<job name & job group>>)
根据InterruptableJob 文档:
由Jobs实现的接口,提供了一种使执行中断的机制。作业不需要实现此接口-实际上,对于大多数人而言,他们的工作都不会。
中断作业在概念上非常相似,并且对Java中的正常中断线程提出了挑战。
实际中断Job的方法必须在Job本身内实现(此接口的interrupt()方法只是调度程序通知Job已请求中断的一种方法)。您的作业用来中断自身的机制可能会因实现方式而异。但是,任何实现中的基本思想都应该是使作业的主体的execute(..)主体定期检查某个标志,以查看是否已请求中断,并且如果设置了该标志,则以某种方式中止其余部分的性能。工作的工作。
强调我的。这是相似但不相同的。您不应该使用Threads(但是确实可以,如果那是您的Job工作...)。
可以在java.org.quartz.examples.DumbInterruptableJob类的源代码中找到中断作业的示例。在中断()和execute(..)中使用wait()和notify()同步的某种组合是合法的,以便使interrupt()方法处于阻塞状态,直到execute(..)表示已注意到该集合为止。旗。