quartz---的Scheduler
从这副图中可以很直观的看出来quartz的关系:
调度:Scheduler任务调度器,是实际执行任务调度的控制器。在spring中通过SchedulerFactoryBean封装起来。
触发器:Trigger 触发器,用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger和NthIncludedDayTrigger,其中CronTrigger用的比较多,本文主要介绍这种方式。CronTrigger在spring中封装在CronTriggerFactoryBean中。
1)scheduler.start();scheduler.standby();用代码解释:
1 package com.imooc.demo.helloQuartz; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.CronScheduleBuilder; 7 import org.quartz.CronTrigger; 8 import org.quartz.JobBuilder; 9 import org.quartz.JobDetail; 10 import org.quartz.Scheduler; 11 import org.quartz.SchedulerException; 12 import org.quartz.SchedulerFactory; 13 import org.quartz.TriggerBuilder; 14 import org.quartz.impl.StdSchedulerFactory; 15 16 public class HelloScheduler { 17 18 public static void main(String[] args) throws SchedulerException, InterruptedException { 19 Date date = new Date(); 20 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 System.out.println("Current Time is:"+sf.format(date)); 22 // 创建一个jobDatail实例,将该实力与helloJob绑定 23 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 24 .build(); 25 26 //美每秒触发一次任务 27 CronTrigger trigger = (CronTrigger)TriggerBuilder 28 .newTrigger() 29 .withIdentity("myTrigger", "group1") 30 //2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次 31 .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?")) 32 .build(); 33 // 创建Scheduler实例 34 SchedulerFactory factory = new StdSchedulerFactory(); 35 Scheduler scheduler = factory.getScheduler(); 36 scheduler.start(); 37 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 38 System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));; 39 Thread.sleep(2000L); 40 //挂起 41 scheduler.standby(); 42 43 //scheduler挂起3秒后继续执行 44 Thread.sleep(3000L); 45 scheduler.start(); 46 } 47 }
每秒执行一次,2秒后停止,执行3次后停止执行,运行结果:
2)shutdown();
1 package com.imooc.demo.helloQuartz; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.CronScheduleBuilder; 7 import org.quartz.CronTrigger; 8 import org.quartz.JobBuilder; 9 import org.quartz.JobDetail; 10 import org.quartz.Scheduler; 11 import org.quartz.SchedulerException; 12 import org.quartz.SchedulerFactory; 13 import org.quartz.TriggerBuilder; 14 import org.quartz.impl.StdSchedulerFactory; 15 16 public class HelloScheduler { 17 18 public static void main(String[] args) throws SchedulerException, InterruptedException { 19 Date date = new Date(); 20 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 System.out.println("Current Time is:"+sf.format(date)); 22 // 创建一个jobDatail实例,将该实力与helloJob绑定 23 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 24 .build(); 25 26 //美每秒触发一次任务 27 CronTrigger trigger = (CronTrigger)TriggerBuilder 28 .newTrigger() 29 .withIdentity("myTrigger", "group1") 30 //2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次 31 .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?")) 32 .build(); 33 // 创建Scheduler实例 34 SchedulerFactory factory = new StdSchedulerFactory(); 35 Scheduler scheduler = factory.getScheduler(); 36 scheduler.start(); 37 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 38 System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));; 39 Thread.sleep(2000L); 40 //挂起 41 scheduler.shutdown(); 42 43 //scheduler挂起3秒后继续执行 44 Thread.sleep(3000L); 45 scheduler.start(); 46 } 47 }
运行结果为:
总结 :
在shutdown()后,程序不能再从新start().
1 package com.imooc.demo.helloQuartz; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.CronScheduleBuilder; 7 import org.quartz.CronTrigger; 8 import org.quartz.JobBuilder; 9 import org.quartz.JobDetail; 10 import org.quartz.Scheduler; 11 import org.quartz.SchedulerException; 12 import org.quartz.SchedulerFactory; 13 import org.quartz.TriggerBuilder; 14 import org.quartz.impl.StdSchedulerFactory; 15 16 public class HelloScheduler { 17 18 public static void main(String[] args) throws SchedulerException, InterruptedException { 19 Date date = new Date(); 20 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 System.out.println("Current Time is:"+sf.format(date)); 22 // 创建一个jobDatail实例,将该实力与helloJob绑定 23 JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob") 24 .build(); 25 26 //美每秒触发一次任务 27 CronTrigger trigger = (CronTrigger)TriggerBuilder 28 .newTrigger() 29 .withIdentity("myTrigger", "group1") 30 //2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次 31 .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?")) 32 .build(); 33 // 创建Scheduler实例 34 SchedulerFactory factory = new StdSchedulerFactory(); 35 Scheduler scheduler = factory.getScheduler(); 36 scheduler.start(); 37 // 把jobDetail和Trigger与scheuler绑定在一起:触发启动 38 System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));; 39 Thread.sleep(2000L); 40 //shutdown(true)标志等待所有正在执行的job执行完之后,再关闭scheduler 41 //shutdown(false)即shutdown()表示job执行完毕之后,再关闭scheduler 42 //shutdown挂起3秒后继续执行 43 scheduler.shutdown(true); 44 System.out.println("scheruler is shut down?:"+scheduler.isShutdown()); 45 46 } 47 }
运行结果:
总结 :
shutdown(true)标志等待所有正在执行的job执行完之后,再关闭scheduler
总结 :
shutdown(false)即关闭scheduler ,然后执行job之后,关掉程序