Quartz管中窥豹之入门小案例


这里记录下Quartz的原生快速入门指南,以内存存储方式,将 trigger 和 job 存储在内存,即配置文件配置:

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

添加Maven依赖

把quartz引入到项目中,添加依赖包

  1. <dependency>
  2.     <groupId>org.quartz-scheduler</groupId>
  3.     <artifactId>quartz</artifactId>
  4.     <version>2.2.1</version>
  5.   </dependency>
  6.   <dependency>
  7.     <groupId>org.quartz-scheduler</groupId>
  8.     <artifactId>quartz-jobs</artifactId>
  9.     <version>2.2.1</version>
  10.   </dependency>

或者将quartz的jar包置于应用classpath路径下,让项目引用。

定义任务

实现Job接口,自定义Job类,实现作业任务,这里主要打印语句

  1. public class HelloJob implements Job {
  2.     private static Logger _log = LoggerFactory.getLogger(HelloJob.class);
  3.     /**
  4.      * <p>
  5.      * Empty constructor for job initilization
  6.      * </p>
  7.      * <p>
  8.      * Quartz requires a public empty constructor so that the
  9.      * scheduler can instantiate the class whenever it needs.
  10.      * </p>
  11.      */
  12.     public HelloJob() {
  13.     }
  14.     /**
  15.      * <p>
  16.      * Called by the <code>{@link org.quartz.Scheduler}</code> when a
  17.      * <code>{@link org.quartz.Trigger}</code> fires that is associated with
  18.      * the <code>Job</code>.
  19.      * </p>
  20.      * 
  21.      * @throws JobExecutionException
  22.      *             if there is an exception while executing the job.
  23.      */
  24.     public void execute(JobExecutionContext context)
  25.         throws JobExecutionException {
  26.         // Say Hello to the World and display the date/time
  27.         _log.info(“Hello Wenqy! – “ + new Date());
  28.     }
  29. }

调度器调度

定义一个简单触发器,以当前时间下一次最近到偶数时分开始触发任务且只触发一次,如现在是08:13:54,那么任务触发时间是08:14:00。然后调度器进行任务调度并启动。

  1. public class SimpleExample {
  2.   public void run() throws Exception {
  3.     Logger log = LoggerFactory.getLogger(SimpleExample.class);
  4.     log.info(“——- Initializing ———————-“);
  5.     // First we must get a reference to a scheduler
  6.     SchedulerFactory sf = new StdSchedulerFactory();
  7.     Scheduler sched = sf.getScheduler();
  8.     log.info(“——- Initialization Complete ———–“);
  9.     // computer a time that is on the next round minute
  10.     Date runTime = evenMinuteDate(new Date());
  11.     log.info(“——- Scheduling Job  ——————-“);
  12.     // define the job and tie it to our HelloJob class
  13.     JobDetail job = newJob(HelloJob.class).withIdentity(“job1”“group1”).build();
  14.     // Trigger the job to run on the next round minute
  15.     Trigger trigger = newTrigger().withIdentity(“trigger1”“group1”).startAt(runTime).build();
  16.     // Tell quartz to schedule the job using our trigger
  17.     sched.scheduleJob(job, trigger);
  18.     log.info(job.getKey() + ” will run at: “ + runTime);
  19.     // Start up the scheduler (nothing can actually run until the
  20.     // scheduler has been started)
  21.     sched.start();
  22.     log.info(“——- Started Scheduler —————–“);
  23.     // wait long enough so that the scheduler as an opportunity to
  24.     // run the job!
  25.     log.info(“——- Waiting 65 seconds… ————-“);
  26.     try {
  27.       // wait 65 seconds to show job
  28.       Thread.sleep(65L * 1000L);
  29.       // executing…
  30.     } catch (Exception e) {
  31.       //
  32.     }
  33.     // shut down the scheduler
  34.     log.info(“——- Shutting Down ———————“);
  35.     sched.shutdown(true);
  36.     log.info(“——- Shutdown Complete —————–“);
  37.   }
  38.   public static void main(String[] args) throws Exception {
  39.     SimpleExample example = new SimpleExample();
  40.     example.run();
  41.   }
  42. }

Cron 表达式

最为常用的CronTirgger,可以用Cron表达式定义任务触发规则,比较强大。CronTirgger 类似于 LINUX 上的任务调度命令 crontab,即利用一个包含7个字段的表达式来表示时间调度方式,非常适合涉及涉及到星期和月份的调度,支持到秒级。如表格定义:

字段 是否必要 允许数值 允许特殊字符 备注
Seconds Yes 0-59 * , – /
Minutes Yes 0-59 * , – /
Hours Yes 0-23 * , – /
Day of month Yes 1-31 * , – ? L W /
Month Yes 1-12 or JAN-DEC * , – /
Day of week Yes 0-6 or SUN-SAT * , – ? L # /
Year No Empty or 1970–2099 * , – / This field is not supported in standard/default implementations.

允许字符含义

* :代表所有可能的值。因此,“*”在Month中表示每个月,在Day-of-Month中表示每天,在Hours表示每小时。

 :表示指定范围,从哪里到哪里,闭包围。

:表示列出枚举值。例如:在Minutes子表达式中,“5,20”表示在5分钟和20分钟触发。

:被用于指定增量。例如:在Minutes子表达式中,“0/15”表示从0分钟开始,每15分钟执行一次。”3/20″表示从第三分钟开始,每20分钟执行一次。和”3,23,43″(表示第3,23,43分钟触发)的含义一样。

? :用在Day-of-Month和Day-of-Week中,指“没有具体的值”。当两个子表达式其中一个被指定了值以后,为了避免冲突,需要将另外一个的值设为“?”。例如:想在每月20日触发调度,不管20号是星期几,只能用如下写法:0 0 0 20 * ?,其中最后以为只能用“?”,而不能用“*”。

L :用在day-of-month和day-of-week字串中。它是单词“last”的缩写。它在两个子表达式中的含义是不同的。
在day-of-month中,“L”表示一个月的最后一天,一月31号,3月30号。
在day-of-week中,“L”表示一个星期的最后一天,也就是“7”或者“SAT”
如果“L”前有具体内容,它就有其他的含义了。例如:“6L”表示这个月的倒数第六天。“FRIL”表示这个月的最后一个星期五。
注意:在使用“L”参数时,不要指定列表或者范围,这样会出现问题。

W :“Weekday”的缩写。只能用在day-of-month字段。用来描叙最接近指定天的工作日(周一到周五)。例如:在day-of-month字段用“15W”指“最接近这个月第15天的工作日”,即如果这个月第15天是周六,那么触发器将会在这个月第14天即周五触发;如果这个月第15天是周日,那么触发器将会在这个月第 16天即周一触发;如果这个月第15天是周二,那么就在触发器这天触发。注意一点:这个用法只会在当前月计算值,不会越过当前月。“W”字符仅能在 day-of-month指明一天,不能是一个范围或列表。也可以用“LW”来指定这个月的最后一个工作日,即最后一个星期五。

:只能用在day-of-week字段。用来指定这个月的第几个周几。例:在day-of-week字段用”5#3″ or “FRI#3″指这个月第3个周五(5指周五,3指第3个)。如果指定的日期不存在,触发器就不会触发。

表达式示例

0 * * * * ? 每1分钟触发一次
0 0 * * * ? 每天每1小时触发一次
0 0 10 * * ? 每天10点触发一次
0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发 
0 30 9 1 * ? 每月1号上午9点半
0 15 10 15 * ? 每月15日上午10:15触发

*/5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 5-15 * * ? 每天5-15点整点触发
0 0/3 * * * ? 每三分钟触发一次
0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 
0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 

0 0 12 ? * WED 表示每个星期三中午12点
0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点
0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 
0 15 10 ? * MON-FRI 周一至周五的上午10:15触发

0 0 23 L * ? 每月最后一天23点执行一次
0 15 10 L * ? 每月最后一日的上午10:15触发 
0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 

0 15 10 * * ? 2005 2005年的每天上午10:15触发 
0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 
0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

Quartz针对他的特性定义了一些demo来演示,可以到官网上看下。

参考:

官网demo:

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/examples/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

11 + 20 = ?