Quartz - CronTrigger Trigger

Please reprint from the source: http://eksliang.iteye.com/blog/2208295

I. Overview

CronTrigger can provide more specific and practical scheduling schemes than SimpleTrigger. Scheduling rules are based on Cron expressions. CronTrigger supports calendar-related repeating time intervals (such as execution on the first Monday of each month) instead of simple periodic time intervals.

2. Introduction to Cron Expressions

1) Cron expression rule table

Quartz uses Cron expressions similar to Linux to define time rules. Cron expressions consist of 6 or 7 time fields separated by spaces. The meaning of each field is shown in the following table:

Field location Location meaning allowance allowed special characters
1  Second  0-59  , - * /
2  minute 0-59 , - * /
3 Hour 0-23 , - * /
4 date 1-31  , - * ? / L W C
5 month 1-12 , - * /
6 Week 1-7  , - * ? / L C #
7 year (optional) Null; 1970-2099  , - * /
2) Special characters in Cron expressions

In addition to allowing numerical values ​​to be set in each field of the Cron expression, some special characters can also be used to provide functions such as lists, ranges, and wildcards. The specific rules are as follows:

  1. Asterisk (*): can be used in all fields, indicating every moment in the corresponding time field, for example, * in the minute field, indicating "every minute";
  2. Question mark (?): This character is only used in date and week fields, it is usually specified as a "nonsense value", equivalent to a dot;
  3. Minus sign (-): Express a range, such as using "10-12" in the hour field, it means from 10 to 12 o'clock, that is, 10,11,12;
  4. Comma (,): Express a list value, such as using "MON, WED, FRI" in the week field, it means Monday, Wednesday and Friday;
  5. Slash (/): x/y expresses a sequence of equal steps, where x is the starting value and y is the incremental step value. If using 0/15 in the minute field means 0, 15, 30 and 45 seconds, and 5/15 in the minute field means 5, 20, 35, 50, you can also use */y which is equivalent to 0/y;
  6. L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;
  7. W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;
  8. LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;
  9. 井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;
  10. C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。

温馨提示:Cron表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。

3)Cron表达式实例

下面表格列出了一些完整的Cron表示式的实例

表示式 说明
"0 0 12 * * ? " 每天12点运行
"0 15 10 ? * *" 每天10:15运行
"0 15 10 * * ?" 每天10:15运行
"0 15 10 * * ? *" 每天10:15运行
"0 15 10 * * ? 2008" 在2008年的每天10:15运行
"0 * 14 * * ?"  每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59。
"0 0/5 14 * * ?" 每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55。
"0 0/5 14,18 * * ?"  每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次。
"0 0-5 14 * * ?"  每天14:00点到14:05,每分钟运行一次。
"0 10,44 14 ? 3 WED" 3月每周三的14:10分到14:44,每分钟运行一次。
"0 15 10 ? * MON-FRI" 每周一,二,三,四,五的10:15分运行。
"0 15 10 15 * ?" 每月15日10:15分运行。
"0 15 10 L * ?" 每月最后一天10:15分运行。
"0 15 10 ? * 6L" 每月最后一个星期五10:15分运行。
"0 15 10 ? * 6L 2007-2009" 在2007,2008,2009年每个月的最后一个星期五的10:15分运行。
"0 15 10 ? * 6#3" 每月第三个星期五的10:15分运行。

 

三.CronTrigger实例

简单的Job实现类:打印任务详情

 

package com.ickes.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
 * 实现Job接口,定义运行的任务
 * @author Ickes
 */
public class SimpleJob implements Job {

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		//打印任务详情
		System.out.println(
				context.getJobDetail().getGroup() 
				+"——"+context.getJobDetail().getName()
				+"——"+context.getTrigger().getName()
				+"——"+context.getTrigger().getGroup());
	}
}

 使用CronTrigger对SimpleJob进行调度,通过Cron表达式制定调度规则,让它每5秒钟运行一次,代码如下:

package com.ickes.job;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Ickes
 */
public class CronTriggerDemo {
	public static void main(String[] args) throws Exception {
		//第一步:创建一个JobDetail实例
		JobDetail jobDetail = new JobDetail("j_job1","j_group1", SimpleJob.class);
		//第二步:创建CronTrigger,指定组及名称,并设置Cron表达式
		CronTrigger cronTrigger = new CronTrigger("t_trigger1", "t_tgroup1");
		CronExpression cexp = new CronExpression("0/5 * * * * ?");//创建表达式
		cronTrigger.setCronExpression(cexp);
		//第三步:通过SchedulerFactory获取一个调度器实例
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		//第四步:将job跟trigger注册到scheduler中进行调度
		scheduler.scheduleJob(jobDetail, cronTrigger);
		//第五步:调度启动
		scheduler.start();
	}
}

 

运行CronTriggerDemo,每5秒钟将触发运行SimpleJob一次。默认情况下Cron表达式对应当前的时区,可以通过CronTrigger的setTimeZone(java.util.TimeZone timeZone)方法显式指定时区。你还也可以通过setStartTime(Date startTime)和setEndTime(Date endTime)指定开始和结束的时间。 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326355110&siteId=291194637