转载地址:https://www.cnblogs.com/qlqwjy/p/8721982.html
所有的Scheduler实例应该由SchedulerFactory来创建
最常用的是StdSchedulerFactory工程类,其出那个键Scheduler的两种方式:
//3. 创建scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 3. 创建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler();
调度器
任务
触发器
1个job对应多个Trigger
4.Scheduler的创建方式
StdSchedulerFactory:
使用一组参数(Java.util.Properties)来创建和初始化Quartz调度器
配置参数一般存储在quartz.properties中
调用getScheduler方法就能创建和初始化调度器对象
// 绑定 jobDetail 和 trigger,将它注册进 Scheduler 当中 ,返回值是最近一次任务执行的开始时间 Date scheduleJob(JobDetail jobDetail, Trigger trigger) // 启动 Scheduler void start() // 暂停 Scheduler void standby() // 关闭 Scheduler void shutdown()
例一:测试:scheduleJob函数返回最近一次开始执行的时间
package cn.qlq.quartz; import static org.quartz.JobBuilder.newJob; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) { try { // 1. 创建一个JodDetail实例 将该实例与Hello job class绑定 (链式写法) JobDetail jobDetail = newJob(HelloJob.class) // 定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("myJob") // 定义name/group .build(); // 打印当前的时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current time is :" + sf.format(date)); // 2. 2018年内每天11点18开始执行,每隔5s执行一次 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1")// 定义名字和组 .withSchedule( //定义任务调度的时间间隔和次数 CronScheduleBuilder .cronSchedule("0/5 0 14,18 * * ? *") ) .build(); // 3. 创建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 4. 将trigger和jobdetail加入这个调度 // scheduler.scheduleJob(jobDetail, trigger); // 5. 启动scheduler scheduler.start(); System.out.println("schedule time is:"+sf.format(scheduler.scheduleJob(jobDetail, trigger))); } catch (Exception e) { e.printStackTrace(); } } }
结果:
current time is :2018-04-05 11:42:42
schedule time is:2018-04-05 02:00:00
例二:测试standby()挂起Scheduler之后3s后再次开启Scheduler
package cn.qlq.quartz; import static org.quartz.JobBuilder.newJob; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) { try { // 1. 创建一个JodDetail实例 将该实例与Hello job class绑定 (链式写法) JobDetail jobDetail = newJob(HelloJob.class) // 定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("myJob") // 定义name/group .build(); // 打印当前的时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current time is :" + sf.format(date)); // 2. 2018年内每天11点18开始执行,每隔5s执行一次 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1")// 定义名字和组 .withSchedule( //定义任务调度的时间间隔和次数 CronScheduleBuilder .cronSchedule("* * * * * ? *") ) .build(); // 3. 创建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 4. 将trigger和jobdetail加入这个调度 scheduler.scheduleJob(jobDetail, trigger); // 5. 启动scheduler scheduler.start(); //scheduler执行2s后挂起 Thread.sleep(2000); scheduler.standby(); //scheduler挂起3s后再次启动scheduler Thread.sleep(3000); scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
结果:
current time is :2018-04-05 11:49:28 current exec time is :2018-04-05 11:49:28 current exec time is :2018-04-05 11:49:29 current exec time is :2018-04-05 11:49:30 current exec time is :2018-04-05 11:49:33 current exec time is :2018-04-05 11:49:33 current exec time is :2018-04-05 11:49:33 current exec time is :2018-04-05 11:49:34 current exec time is :2018-04-05 11:49:35 ....
例三:测试shutdown函数(shutdown之后不能再次调用start重启,会报错)
shutdown(true)表示等待所有正在执行的任务执行完毕后关闭Scheduler
shutdown(false),即shutdown()表示直接关闭Scheduler
(1)Job中睡眠5s钟
package cn.qlq.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //打印当前的时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current exec time is :"+sf.format(date)); } }
shutdown传入true参数:(在程序执行完之后才杀死Scheduler)
package cn.qlq.quartz; import static org.quartz.JobBuilder.newJob; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) { try { // 1. 创建一个JodDetail实例 将该实例与Hello job class绑定 (链式写法) JobDetail jobDetail = newJob(HelloJob.class) // 定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("myJob") // 定义name/group .build(); // 打印当前的时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current time is :" + sf.format(date)); // 2. 2018年内每天11点18开始执行,每隔5s执行一次 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1")// 定义名字和组 .withSchedule( //定义任务调度的时间间隔和次数 CronScheduleBuilder .cronSchedule("* * * * * ? *") ) .build(); // 3. 创建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 4. 将trigger和jobdetail加入这个调度 scheduler.scheduleJob(jobDetail, trigger); // 5. 启动scheduler scheduler.start(); //scheduler执行2s后挂起 Thread.sleep(2000); scheduler.shutdown(true); //shutdown(true)表示等待所有正在执行的任务执行完毕后关闭Scheduler //shutdown(false),即shutdown()表示直接关闭Scheduler System.out.println("scheduler is shutdown?"+scheduler.isShutdown()); } catch (Exception e) { e.printStackTrace(); } } }
结果:
current time is :2018-04-05 11:58:25 current exec time is :2018-04-05 11:58:30 current exec time is :2018-04-05 11:58:31 current exec time is :2018-04-05 11:58:32 scheduler is shutdown?true
shutdown传入false参数:(立即杀死Scheduler)
package cn.qlq.quartz; import static org.quartz.JobBuilder.newJob; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) { try { // 1. 创建一个JodDetail实例 将该实例与Hello job class绑定 (链式写法) JobDetail jobDetail = newJob(HelloJob.class) // 定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("myJob") // 定义name/group .build(); // 打印当前的时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current time is :" + sf.format(date)); // 2. 2018年内每天11点18开始执行,每隔5s执行一次 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1")// 定义名字和组 .withSchedule( //定义任务调度的时间间隔和次数 CronScheduleBuilder .cronSchedule("* * * * * ? *") ) .build(); // 3. 创建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 4. 将trigger和jobdetail加入这个调度 scheduler.scheduleJob(jobDetail, trigger); // 5. 启动scheduler scheduler.start(); //scheduler执行2s后挂起 Thread.sleep(2000); scheduler.shutdown(false); //shutdown(true)表示等待所有正在执行的任务执行完毕后关闭Scheduler //shutdown(false),即shutdown()表示直接关闭Scheduler System.out.println("scheduler is shutdown?"+scheduler.isShutdown()); } catch (Exception e) { e.printStackTrace(); } } }
结果:
current time is :2018-04-05 12:00:11 scheduler is shutdown?true current exec time is :2018-04-05 12:00:17 current exec time is :2018-04-05 12:00:17 current exec time is :2018-04-05 12:00:18 current exec time is :2018-04-05 12:00:19