2.初识CronTrigger

开发工具:Eclipse

代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git

前言:

1.初始Quartz里面,我们介绍了quartz的一种简单使用,就是精确的时间去控制任务的调度,

例如,多久后执行任务,多久后结束之类的。

这种模式并不能体现Quartz的强大,

比如,我想实现:

在每周一早上8:00给女朋友发条问候信息、在她的生日那天0:00发一条祝福短信……

quartz可以帮我们做到这些。

cron表达式:

quartz实现了对cron表达式的支持,那么整个任务调度就可以基于日历、日期去操作执行,而不是简单的用时间刻度去控制。

这样,会更加灵活。

这里将不再讲解cron表达式,有兴趣的可以来到这里学习Cron表达式:

 3.初始Cron表达式


任务一:

每天10:43:30执行一次:

1.定义CronHelloScheduler:

Cron表达式:“30 43 10 * * ?

 1 package com.xm.scheduler;
 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.SimpleScheduleBuilder;
14 import org.quartz.Trigger;
15 import org.quartz.TriggerBuilder;
16 import org.quartz.impl.StdSchedulerFactory;
17 
18 import com.xm.job.HelloJob;
19 
20 public class CronHelloScheduler {
21 
22     public static void main(String[] args) throws SchedulerException {
23         /**
24          * 1.定义一个JobDetail
25          *2.绑定 HelloJob
26          *3.定义id,group
27          */
28         JobDetail jobDetail = JobBuilder
29                 .newJob(HelloJob.class)
30                 .withIdentity("job1", "group1")
31                 .build();
32         
33         /**
34          * 1.获取系统时间
35          * 2.定义开始时间
36          * 3.定义结束时间
37          */
38         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
39         Date nowTime = new Date();
40         System.out.println("Now Time:"+sf.format(nowTime));
41         /*Date startTime = new Date(nowTime.getTime()+5000);
42         System.out.println("Start Time:"+sf.format(startTime));
43         Date endTime = new Date(nowTime.getTime()+30000);
44         System.out.println("End Time:"+sf.format(endTime));*/
45         
46         /**
47          * 1.定义一个CronTrigger
48          * 2.绑定group,name
49          * 3.定义cron表达式规则
50          */
51         CronTrigger trigger = (CronTrigger)TriggerBuilder
52                 .newTrigger()
53                 .withIdentity("trigger2", "group1")
54                 .withSchedule(
55                         CronScheduleBuilder
56                         //定义cron表达式规则
57                         .cronSchedule("30 43 10 * * ?")
58                         )
59                 .build();
60         
61         /**
62          * 1.定义一个SchedulerFactory工厂类
63          * 2.获得一个Scheduler类
64          * 3.启动Scheduler
65          * 4.绑定JobDetail和Trigger
66          */
67         //定义一个SchedulerFactory工厂类
68         SchedulerFactory  factory = new StdSchedulerFactory() ;
69         //获得一个Scheduler类
70         Scheduler scheduler = factory.getScheduler();
71         //启动Scheduler
72         scheduler.start();
73         //绑定JobDetail和Trigger
74         scheduler.scheduleJob(jobDetail, trigger);    
75         
76     }
77 
78 }

 

2.运行结果:

3.代码下载链接:https://github.com/theIndoorTrain/QuartzDemo/tree/5ab064d06f7a0de92de21c84b660366d39cbb859


任务二:

每个周六的上午10:50—10:52每隔11s执行一次

1.修改cron表达式:

Cron表达式:”0/11 50-52 10 ? * 7″

 1 package com.xm.scheduler;
 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.SimpleScheduleBuilder;
14 import org.quartz.Trigger;
15 import org.quartz.TriggerBuilder;
16 import org.quartz.impl.StdSchedulerFactory;
17 
18 import com.xm.job.HelloJob;
19 
20 public class CronHelloScheduler {
21 
22     public static void main(String[] args) throws SchedulerException {
23         /**
24          * 1.定义一个JobDetail
25          *2.绑定 HelloJob
26          *3.定义id,group
27          */
28         JobDetail jobDetail = JobBuilder
29                 .newJob(HelloJob.class)
30                 .withIdentity("job1", "group1")
31                 .build();
32         
33         /**
34          * 1.获取系统时间
35          * 2.定义开始时间
36          * 3.定义结束时间
37          */
38         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
39         Date nowTime = new Date();
40         System.out.println("Now Time:"+sf.format(nowTime));
41         /*Date startTime = new Date(nowTime.getTime()+5000);
42         System.out.println("Start Time:"+sf.format(startTime));
43         Date endTime = new Date(nowTime.getTime()+30000);
44         System.out.println("End Time:"+sf.format(endTime));*/
45         
46         /**
47          * 1.定义一个CronTrigger
48          * 2.绑定group,name
49          * 3.定义cron表达式规则
50          */
51         CronTrigger trigger = (CronTrigger)TriggerBuilder
52                 .newTrigger()
53                 .withIdentity("trigger2", "group1")
54                 .withSchedule(
55                         CronScheduleBuilder
56                         //定义cron表达式规则
57                         .cronSchedule("0/11 50-52 10 ? * 7")
58                         )
59                 .build();
60         
61         /**
62          * 1.定义一个SchedulerFactory工厂类
63          * 2.获得一个Scheduler类
64          * 3.启动Scheduler
65          * 4.绑定JobDetail和Trigger
66          */
67         //定义一个SchedulerFactory工厂类
68         SchedulerFactory  factory = new StdSchedulerFactory() ;
69         //获得一个Scheduler类
70         Scheduler scheduler = factory.getScheduler();
71         //启动Scheduler
72         scheduler.start();
73         //绑定JobDetail和Trigger
74         scheduler.scheduleJob(jobDetail, trigger);    
75         
76     }
77 
78 }

2.运行截图:

3.代码下载链接:https://github.com/theIndoorTrain/QuartzDemo/tree/a1c93071a16488383ff3332d042dcf537f05c972


                                                                                                                                                                                                                                                   2018-07-14