开发工具:STS
代码下载链接:
前言:
接触一个新事物的开始,我们都会产生一些疑问:
Quartz是什么?
Quartz是用来做什么的?
答:
Quartz是一个任务调度框架。说明白点,就是定制任务的。
例如:
每年你过生日时给你发一张生日贺卡;
每周一的6:00给你发一条起床提醒;
每月的15号给你发工资.......
明白了quartz的原理,我们就来进入我们的学习。
项目搭建
1.新建maven项目
2.导入quartz相关依赖
1 2 34 org.quartz-scheduler 5quartz 62.3.0 7
任务一:
每隔五秒打印一句“Hello world!”;
1.新建一个Job类实现Job接口:
实现:打印系统时间,输出“Hello world!”
1 package com.xm.job; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.Job; 7 import org.quartz.JobExecutionContext; 8 import org.quartz.JobExecutionException; 9 10 public class HelloJob implements Job{11 12 13 public void execute(JobExecutionContext context) throws JobExecutionException {14 Date date = new Date();15 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");16 System.out.println("Job start time: "+sf.format(date));17 System.out.println("Hello world!");18 }19 20 }
2.定义一个Scheduler类:
实现:
(1)定义一个JobDetail,绑定Job
(2)定义一个Trigger,定义任务细节
(3)定义一个Scheduler,绑定JobDetail和Trigger
1 package com.xm.scheduler; 2 3 import org.quartz.JobBuilder; 4 import org.quartz.JobDetail; 5 import org.quartz.Scheduler; 6 import org.quartz.SchedulerException; 7 import org.quartz.SchedulerFactory; 8 import org.quartz.SimpleScheduleBuilder; 9 import org.quartz.Trigger;10 import org.quartz.TriggerBuilder;11 import org.quartz.impl.StdSchedulerFactory;12 13 import com.xm.job.HelloJob;14 15 public class HelloScheduler {16 17 public static void main(String[] args) throws SchedulerException {18 /**19 * 1.定义一个JobDetail20 *2.绑定 HelloJob21 *3.定义id,group22 */23 JobDetail jobDetail = JobBuilder24 .newJob(HelloJob.class)25 .withIdentity("job1", "group1")26 .build();27 28 /**29 * 1.定义一个trigger30 * 2.绑定group,name31 * 3.指定开始、结束时间32 * 4.定义任务细节33 */34 Trigger trigger = TriggerBuilder35 .newTrigger()36 //绑定group,name37 .withIdentity("trigger1", "group1")38 //从现在开始执行39 .startNow()40 //指定开始执行时间41 //.startAt()42 //指定结束执行时间43 //.endAt()44 .withSchedule(45 SimpleScheduleBuilder.simpleSchedule()46 //执行间隔时间47 .withIntervalInSeconds(5)48 //重复执行49 .repeatForever()50 )51 .build();52 53 /**54 * 1.定义一个SchedulerFactory工厂类55 * 2.获得一个Scheduler类56 * 3.启动Scheduler57 * 4.绑定JobDetail和Trigger58 */59 //定义一个SchedulerFactory工厂类60 SchedulerFactory factory = new StdSchedulerFactory() ;61 //获得一个Scheduler类62 Scheduler scheduler = factory.getScheduler();63 //启动Scheduler64 scheduler.start();65 //绑定JobDetail和Trigger66 scheduler.scheduleJob(jobDetail, trigger); 67 68 }69 70 }
3.运行结果:
4.代码下载链接:
任务二:
每隔五秒打印一句“Hello world!”,5s后开始,30秒后结束
1.修改Scheduler:
1 package com.xm.scheduler; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.JobBuilder; 7 import org.quartz.JobDetail; 8 import org.quartz.Scheduler; 9 import org.quartz.SchedulerException;10 import org.quartz.SchedulerFactory;11 import org.quartz.SimpleScheduleBuilder;12 import org.quartz.Trigger;13 import org.quartz.TriggerBuilder;14 import org.quartz.impl.StdSchedulerFactory;15 16 import com.xm.job.HelloJob;17 18 public class HelloScheduler {19 20 public static void main(String[] args) throws SchedulerException {21 /**22 * 1.定义一个JobDetail23 *2.绑定 HelloJob24 *3.定义id,group25 */26 JobDetail jobDetail = JobBuilder27 .newJob(HelloJob.class)28 .withIdentity("job1", "group1")29 .build();30 31 /**32 * 1.获取系统时间33 * 2.定义开始时间34 * 3.定义结束时间35 */36 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");37 Date nowTime = new Date();38 System.out.println("Now Time:"+sf.format(nowTime));39 Date startTime = new Date(nowTime.getTime()+5000);40 System.out.println("Start Time:"+sf.format(startTime));41 Date endTime = new Date(nowTime.getTime()+30000);42 System.out.println("End Time:"+sf.format(endTime));43 44 /**45 * 1.定义一个trigger46 * 2.绑定group,name47 * 3.指定开始、结束时间48 * 4.定义任务细节49 */50 Trigger trigger = TriggerBuilder51 .newTrigger()52 //绑定group,name53 .withIdentity("trigger1", "group1")54 //从现在开始执行55 //.startNow()56 //指定开始执行时间57 .startAt(startTime)58 //指定结束执行时间59 .endAt(endTime)60 .withSchedule(61 SimpleScheduleBuilder.simpleSchedule()62 //执行间隔时间63 .withIntervalInSeconds(5)64 //重复执行65 .repeatForever()66 )67 .build();68 69 /**70 * 1.定义一个SchedulerFactory工厂类71 * 2.获得一个Scheduler类72 * 3.启动Scheduler73 * 4.绑定JobDetail和Trigger74 */75 //定义一个SchedulerFactory工厂类76 SchedulerFactory factory = new StdSchedulerFactory() ;77 //获得一个Scheduler类78 Scheduler scheduler = factory.getScheduler();79 //启动Scheduler80 scheduler.start();81 //绑定JobDetail和Trigger82 scheduler.scheduleJob(jobDetail, trigger); 83 84 }85 86 }
2.运行结果:
3.代码下载链接:
2018-07-14