1.Job part
Job actually made up of three parts:
JobDetail: This Job is used to describe what to do
to achieve Job categories: specific work of
JobDataMap: to provide parameters for the Job
In addition to usingJobData JobDataMap way, it may also be other ways
Concurrent 2.Job
By default, regardless of the last time the task is completed or completed, as long as the specified time is up, then the next time start.
Sometimes long time to do tasks, such as database backup, this time after a backup on the successful conclusion of hope, began at once to back up, even to the specified time, can not start, because this is likely to cause the database is locked (several threads at the same time the backup database, causing unpredictable chaos).
So in this case, the database backup task to add a comment like:
3.Job abnormal
Task happened abnormalities are very common. Abnormal approach usually two ways:
1. When an exception occurs, then notice all this Job scheduling management, it stops running
2. When an exception occurs, modify parameters and immediately re-run
public class ExceptionJob1 the implements the Job { @Override public void Execute (the JobExecutionContext JobExecutionContext) throws JobExecutionException { int I = 0 ; the try { // abnormality occurs deliberately System.out.println (100 / I); } the catch (Exception E) { the System. Out.println ( "exception occurs, cancel all scheduled corresponding to the Job" ); JobExecutionException JE = new new JobExecutionException (E); JE. setUnscheduleAllTriggers(true); throw je; } } }
public class ExceptionJob2 the implements the Job { static int I = 0 ; @Override public void Execute (the JobExecutionContext JobExecutionContext) throws JobExecutionException { the try { // abnormality occurs deliberately System.out.println ( "calculation result" 100 + / I); } the catch ( E exception) { System.out.println ( "exception occurs, modify the parameters, immediately re-execute" ); I =. 1 ; JobExecutionException JE = new newJobExecutionException (E); . JE setRefireImmediately ( to true ); // execute the job immediately re the throw JE; } } }
public class ExceptionTest { public static void main(String[] args) throws Exception{ exceptionHandle1(); exceptionHandle2(); } private static void exceptionHandle1() throws Exception { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(2) .withRepeatCount ( 10 )) .build (); // define a the JobDetail . newJob the JobDetail Job = (ExceptionJob1 class ) .withIdentity ( "exceptionJob1", "someJobGroup" ) .build (); // scheduled to join the Job scheduler.scheduleJob (job, Trigger); // start scheduler.start (); // after waiting 20 seconds, so that the preceding tasks are performed over and then close scheduler the Thread.sleep (20000 ); scheduler.shutdown ( to true ); }
Private static void exceptionHandle2 ()throws Exception { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(2) .withRepeatCount(10)) .build(); //定义一个JobDetail JobDetail job = newJob(ExceptionJob2.class) .withIdentity("exceptionJob1", "someJobGroup") .build(); // dispatch to join the Job scheduler.scheduleJob (the Job, the Trigger); // start scheduler.start (); // wait 20 seconds, so that the task ahead after performing all over, and then close the scheduler Thread.sleep (20000 ); scheduler.shutdown ( to true ); } }
4. Interrupt Job
In business, sometimes you need to interrupt the task, then the need to implement the Job InterruptableJob the interface, and then easily interrupted
public class StoppableJob implements InterruptableJob { private boolean stop = false; @Override public void interrupt() throws UnableToInterruptJobException { System.out.println("被调度叫停"); stop = true; } @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { while(true){ if(STOP) BREAK ; the try { System.out.println ( "every second, once detected, to see if the stop" ); the Thread.sleep ( 1000 ); } the catch (InterruptedException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } System.out.println ( "continue work ..." ); } } }
test
public class Stop { public static void main(String[] args) throws Exception{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .startNow() .build(); //定义一个JobDetail JobDetail job = newJob(StoppableJob.class) .withIdentity("exceptionJob1", "someJobGroup") .build(); //调度加入这个job scheduler.scheduleJob (Job, Trigger); // start scheduler.start (); the Thread.sleep ( 5000 ); System.out.println ( "5 seconds, stop scheduling Job" ); // Key is equivalent to the Job primary key scheduler.interrupt (job.getKey ()); // wait 20 seconds, so that the preceding tasks are performed after the expiry, the scheduler then close the Thread.sleep (20000 ); scheduler.shutdown ( to true ); } }