好记性不如烂博客之 Quartz InterruptableJob 可拦截任务
我们的具体任务类继承InterruptableJob接口 就可以实现对任务方法的拦截
public interface InterruptableJob extends Job { void interrupt() throws UnableToInterruptJobException; }
实现类
package com.task.demo.example6; import java.util.Date; import org.quartz.InterruptableJob; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.UnableToInterruptJobException; public class DumbInterruptableJob implements InterruptableJob { // has the job been interrupted? private boolean _interrupted = false; // job name private JobKey _jobKey = null; public DumbInterruptableJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { _jobKey = context.getJobDetail().getKey(); System.out.println("---- " + _jobKey + " executing at " + new Date()); try { // main job loop... see the JavaDOC for InterruptableJob for discussion... // do some work... in this example we are 'simulating' work by sleeping... :) for (int i = 0; i < 4; i++) { try { Thread.sleep(1000L); } catch (Exception ignore) { ignore.printStackTrace(); } // periodically check if we've been interrupted... if(_interrupted) { System.out.println("--- " + _jobKey + " -- Interrupted... bailing out!"); return; // could also choose to throw a JobExecutionException // if that made for sense based on the particular // job's responsibilities/behaviors } } } finally { System.out.println("---- " + _jobKey + " completed at " + new Date()); } } public void interrupt() throws UnableToInterruptJobException { System.out.println("---" + _jobKey + " -- INTERRUPTING --"); _interrupted = true; } }
测试
package com.task.demo.example6; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.DateBuilder.*; import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class InterruptExample { public void run() throws Exception { // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); // get a "nice round" time a few seconds in the future... Date startTime = nextGivenSecondDate(null, 2); JobDetail job = newJob(DumbInterruptableJob.class) .withIdentity("interruptableJob1", "group1") .build(); SimpleTrigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startAt(startTime) .withSchedule(simpleSchedule() .withIntervalInSeconds(5) .repeatForever()) .build(); Date ft = sched.scheduleJob(job, trigger); System.out.println(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // start up the scheduler (jobs do not start to fire until // the scheduler has been started) sched.start(); for(int i=0; i < 50; i++) { try { Thread.sleep(7000L); // tell the scheduler to interrupt our job sched.interrupt(job.getKey()); } catch (Exception e) { } } sched.shutdown(true); } public static void main(String[] args) throws Exception { InterruptExample example = new InterruptExample(); example.run(); } }