Overview Sample Sample source code
Overview
Stop the task that is being performed in quartz due to business needs
The task class only needs to implement the Interruptablejob class and then implement the interrupt () method.
In this method, the markup changes, in the execution of this tag to determine, you can achieve the interruption of the task
Also invoke method on dispatcher: Sched.interrupt (Job.getkey ()) example
Job class
Package com.xgj.quartz.quartzItself.interruptableJob;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.quartz.InterruptableJob;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
Import Org.quartz.JobKey;
Import org.quartz.UnableToInterruptJobException;
/** * * * @ClassName: Dumbinterruptablejob * * @Description: An executable interrupt-execution program for unit testing. * * @author: Mr.yang * * @date: November 15, 2017 morning 9:26:36 * * public class Dumbinterruptablejob implements Interruptable
Job {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Private Boolean _interrupted = false; Whether the job interrupts private jobkey _jobkey = null; Job name private static int counts = 0;
Interrupt execution Count @Override public void execute (jobexecutioncontext context) throws Jobexecutionexception {
_jobkey = Context.getjobdetail (). Getkey (); System.out.println ("Start Execution" task key: "+ _jobkey +", Execution time: "+ sdf.formAt (new Date ());
try {for (int i = 0; i < 4; i++) {try {thread.sleep (1000L);
catch (Exception e) {e.printstacktrace ();
//See if Interrupt if (_interrupted) {counts++;
System.out.println ("By outside factors stopped this task key:" + _jobkey + ", interrupt cumulative number of times:" + counts + "\ n"); Return You can also choose to throw a jobexecutionexception, specify the behavior according to the business needs}}} finally {SYSTEM.OUT.P
RINTLN ("Complete task" key: + _jobkey + "Finish time:" + Sdf.format (new Date ()); @Override public void Interrupt () throws Unabletointerruptjobexception {System.out.println ("\ n
————— "Interrupt" the outside world is calling the scheduler to stop this task key: "+ _jobkey +" ———————— ");
_interrupted = true;
}
}
Scheduling class
Package com.xgj.quartz.quartzItself.interruptableJob;
Import static org.quartz.DateBuilder.nextGivenSecondDate;
Import static Org.quartz.JobBuilder.newJob;
Import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
Import static Org.quartz.TriggerBuilder.newTrigger;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import Org.quartz.SchedulerFactory;
Import Org.quartz.SchedulerMetaData;
Import Org.quartz.SimpleTrigger;
Import Org.quartz.impl.StdSchedulerFactory; /** * * * @ClassName: Interruptexample * * @Description: Dispatch class * * @author: Mr.yang * * * @date: November 15, 2017 morning 9 : 28:21 */public class Interruptexample {public void run () throws Exception {SimpleDateFormat SDF = new S
Impledateformat ("Yyyy-mm-dd HH:mm:ss");
SYSTEM.OUT.PRINTLN ("-------initialization----------------------");
Schedulerfactory SF = new Stdschedulerfactory ();
Scheduler sched = Sf.getscheduler (); Next 15 seconds Date starttime = nextgivenseconddate (null, 15); 15 seconds after the current time, perform a task every 5 seconds jobdetail job = Newjob (Dumbinterruptablejob.class). Withidentity ("Interrup
TableJob1 "," group1 "). Build (); Simpletrigger trigger = Newtrigger (). Withidentity ("Trigger1", "group1"). StartAt (Starttim
e). Withschedule (Simpleschedule (). Withintervalinseconds (5)
. RepeatForever ()). build ();
Date ft = sched.schedulejob (job, trigger); System.out.println (Job.getkey () + "will run on:" + sdf.format (ft) + "and repeat:" + trigger.getrepeatcount () + "times, Interval
"+ trigger.getrepeatinterval ()/1000 +" seconds ");
Dispatch starts execution Sched.start ();
System.out.println ("-------Start dispatch (Invoke. Start () method)----------------");
System.out.println ("-------Start an interrupt task (10 interrupts) every 7 seconds----------");
for (int i = 0; i < i++) { try {thread.sleep (7000L);
Manually interrupt the Job Sched.interrupt (Job.getkey ()) in the scheduler;
catch (Exception e) {e.printstacktrace ();
} System.out.println ("-------Shutdown scheduling---------------------");
Sched.shutdown (TRUE);
System.out.println ("-------Shutdown Scheduler completes-----------------");
Schedulermetadata metaData = Sched.getmetadata ();
System.out.println ("~~~~~~~~~~ executed" + metadata.getnumberofjobsexecuted () + "jobs.");}
public static void Main (string[] args) throws Exception {Interruptexample example = new Interruptexample ();
Example.run ();
}
}
Run results
-------Initialization----------------------Info stdschedulerfactory-using default implementation for Threadexecutor info Simpl Ethreadpool-job execution threads'll use class loader of Thread:main INFO schedulersignalerimpl-initialized Ler signaller of Type:class org.quartz.core.SchedulerSignalerImpl INFO quartzscheduler-quartz Scheduler v.2.2.3 Create
D. INFO Ramjobstore-ramjobstore initialized. INFO quartzscheduler-scheduler Meta-data:quartz Scheduler (v2.2.3) ' Defaultquartzscheduler ' with Instanceid ' non_clust
Ered ' Scheduler class: ' Org.quartz.core.QuartzScheduler '-running locally.
Not started.
Currently in standby mode.
Number of jobs executed:0 Using thread pool ' org.quartz.simpl.SimpleThreadPool '-with threads. Using job-store ' Org.quartz.simpl.RAMJobStore '-which does not support persistence.
and is not clustered. INFO Stdschedulerfactory-quartz Scheduler ' Defaultquartzscheduler ' initialized from default resource file in Quartz PAC Kage:' Quartz.properties ' INFO Stdschedulerfactory-quartz Scheduler version:2.2.3 GROUP1.INTERRUPTABLEJOB1 will run in: 2017-11-15
09:29:45 and repeat:-1 times, interval 5 seconds INFO Quartzscheduler-scheduler defaultquartzscheduler_$_non_clustered started. -------Start the dispatch (Invoke the. Start () method)-----------------------Start an interrupt task (10 interrupts) every 7 seconds----------"Start Execution" Task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:29:45 —————— "Interrupt" the outside world is calling the scheduler to stop this task Key:group1.interruptableJob1 ——— ————— was stopped by external factors. KEY:GROUP1.INTERRUPTABLEJOB1:1 "complete task" KEY:GROUP1.INTERRUPTABLEJOB1 completion time: 2017-11-15 09:29:49 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:29:50 "complete the task" Key:group1.interruptableJob1 Completion time: 2017-11-15 09:29:54 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:29:55 —————— "Break" The outside world is calling the scheduler to stop this task. Key:group1.interruptableJob1 ———————— was stopped by outside forces. KEY:GROUP1.INTERRUPTABLEJOB1, break cumulative number of times: 2 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 Finish time: 2017-11-15 09:29:56 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:00 —————— "Interrupt" the outside is calling the scheduler to stopThis task Key:group1.interruptableJob1 ———————— been stopped by external factors key:group1.interruptableJob1, the number of interrupts: 3 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 Finish time: 2017-11-15 09:30:03 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:05 Finish Task Key:group1.interruptableJob1 time: 2017-11-15 09:30:09 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:10 "Complete the Task" Key:group1.interruptableJob1 time: 2017-11-15 09:30:14 "Start Execution" Task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:15 —————— "Interrupt" the outside world is calling the scheduler to stop this task Key:group1.interruptableJob1 ——— ————— was stopped by external factors. KEY:GROUP1.INTERRUPTABLEJOB1:4 "complete task" KEY:GROUP1.INTERRUPTABLEJOB1 completion time: 2017-11-15 09:30:17 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:20 —————— "Break" The outside world is calling the scheduler to stop this task. Key:group1.interruptableJob1 ———————— was stopped by outside forces. KEY:GROUP1.INTERRUPTABLEJOB1, break cumulative number of times: 5 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 Finish time: 2017-11-15 09:30:24 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:25 Finish Task Key:group1.interruptableJob1 time: 2017-11-15 09:30: 29 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:30 —————— "Interrupt" The outside world is calling the scheduler to stop this task. Key:group1.interruptableJob1 ———————— was stopped by outside forces. KEY:GROUP1.INTERRUPTABLEJOB1, break cumulative number of times: 6 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 Finish time: 2017-11-15 09:30:31 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:35 —————— "Interrupt" the outside world is calling the scheduler to stop this task Key:group1.interruptableJob1 ———————— was stopped by external factors key:group1.interruptableJob1, Cumulative number of interrupts: 7 Finish task Key:group1.interruptableJob1 time: 2017-11-15 09:30:38 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:40 "Complete the Task" Key:group1.interruptableJob1 time: 2017-11-15 09:30:44 "Start Execution" Task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:45 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 completion time: 2017-11-15 09:30:49 "Start Execution" task Key:group1.interruptableJob1, Execution time: 2017-11-15 09:30:50 —————— "Break" The outside world is calling the scheduler to stop this task Key:group1.interruptableJob1 ————————-------shutdown Dispatch---------------------INFO Quartzscheduler-
Scheduler defaultquartzscheduler_$_non_clustered shutting down. INFO QuartzschedulEr-scheduler defaultquartzscheduler_$_non_clustered paused. By external factors stopped the task Key:group1.interruptableJob1, broken cumulative number of times: 8 "complete the task" KEY:GROUP1.INTERRUPTABLEJOB1 completion time: 2017-11-15 09:30:52 INFO
Quartzscheduler-scheduler defaultquartzscheduler_$_non_clustered shutdown complete.
-------Shutdown Scheduler completed-----------------~~~~~~~~~~ executed 14 jobs.
Sample source Code
Code is hosted to Github-> Https://github.com/yangshangwei/SpringMaster