Quartz-CronTrigger解读

在这里插入图片描述

概述

CronTrigger 比 SimpleTrigger 更有用,如果你需要基于日期的概念来触发任务的话,可以使用 CronTrigger。

使用 CronTrigger,你可以指定以下的这些日期:“每周五中午”,或“每天上午 9:30”,或者“每周一上午 9:00 到 10:00 每 5 分钟,一月的每个周四和周五”。

虽然如此,就像 SimpleTrigger 一样,CronTrigger 也可以设置 startTime 和 endTime。


Cron表达式的组成

Cron 表达式用于配置 CornTrigger 实例。Cron 表达式是一个字符串,由 7 个子表达式组成,这 7 个部分用空格分隔,它们分别表示:

  1. 分钟
  2. 小时
  3. 月份
  4. 星期几
  5. 年(可选字段)

Cron Schedule用的了Cron表达式**【秒】【分】【时】【日】【月】【周】【年】**

例如,Cron 表达式 “0 0 12 ? * WED” 表示“每周三下午 12:00:00”。


cron表达式中的问号(?) 的含义

问号(?)的作用是指明该字段‘没有特定的值’

其实这也很好理解,比如:“0/5 * * 3 * SUN”,既指定了每个月的3号,并且又指定了每个星期天,那么并不可能存在每个3号都是星期天的可能性,所以,表达式也就不成立了。

cronExpression对日期星期字段的处理规则是它们必须互斥,即只能且必须有一个字段有特定的值,另一个字段必须是‘没有特定的值’;

1、当星期和日期都为*或数字时,报错

Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.

即两个字段不能都指明的特定的值,必须互斥。这里的*和数字是一样的,如果都指明特定的数字,也是报一样的错。

2、当星期和日期都为?时,报错

'?' can only be specfied for Day-of-Month -OR- Day-of-Week.

即两个字段不能都‘没有特定的值’。


时间格式

这里写图片描述


特殊字符

特殊字符含义
*字符可以用于所有字段,在“分”字段中设为"*"表示"每一分钟"的含义
?字符可以用在“日”和“周几”字段. 它用来指定 ‘不明确的值’. 这在你需要指定这两个字段中的某一个值而不是另外一个的时候会被用到
-字符被用来指定一个值的范围,比如在“小时”字段中设为"10-12"表示"10点到12点"
,字符指定数个值。比如在“周几”字段中设为"MON,WED,FRI"表示"the days Monday, Wednesday, and Friday"
/字符用来指定一个值的的增加幅度,比如在“秒”字段中设置为"0/15"表示"第0, 15, 30, 和 45秒"。而 “5/15"则表示"第5, 20, 35, 和 50”. 在’/'前加"*“字符相当于指定从0秒开始. 每个字段都有一系列可以开始或结束的数值。对于“秒”和“分”字段来说,其数值范围为0到59,对于“小时”字段来说其为0到23, 对于“日”字段来说为0到31, 而对于“月”字段来说为0到11。”/"字段仅仅只是帮助你在允许的数值范围内从开始"第n"的值。 因此对于“月”字段来说"7/6"只是表示7月被开启而不是“每六个月”, 请注意其中微妙的差别
L字符可用在“日”和“周几”这两个字段。它是"last"的缩写, 但是在这两个字段中有不同的含义。例如,“日”字段中的"L"表示"一个月中的最后一天" —— 对于一月就是31号对于二月来说就是28号(非闰年)。而在“周几”字段中, 它简单的表示"7" or “SAT”,但是如果在“周几”字段中使用时跟在某个数字之后, 它表示"该月最后一个星期×" —— 比如"6L"表示"该月最后一个周五"。当使用’L’选项时,指定确定的列表或者范围非常重要,否则你会被结果搞糊涂的
W可用于“日”字段。用来指定历给定日期最近的工作日(周一到周五) 。比如你将“日”字段设为"15W",意为: “离该月15号最近的工作日”。因此如果15号为周六,触发器会在14号即周五调用。如果15号为周日, 触发器会在16号也就是周一触发。如果15号为周二,那么当天就会触发。然而如果你将“日”字段设为"1W", 而一号又是周六, 触发器会于下周一也就是当月的3号触发,因为它不会越过当月的值的范围边界。'W’字符只能用于“日”字段的值为单独的一天而不是一系列值的时候
LW可以组合用于“日”字段表示为’LW’,意为"该月最后一个工作日"
#字符可用于“周几”字段。该字符表示“该月第几个周×”,比如"6#3"表示该月第三个周五( 6表示周五而"#3"该月第三个)。再比如: “2#1” = 表示该月第一个周一而 “4#5” = 该月第五个周三。注意如果你指定"#5"该月没有第五个“周×”,该月是不会触发的
C字符可用于“日”和“周几”字段,它是"calendar"的缩写。它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日历, 那它等同于包含全部日历。“日”字段值为"5C"表示"日历中的第一天或者5号以后",“周几”字段值为"1C"则表示"日历中的第一天或者周日以后"

Cron 表达式样例

CronTrigger 样例 1 – 时钟从 0 分钟开始,每 5 分钟执行一次

0 0/5 * * * ?

CronTrigger 样例 2 – 时钟从 0 分钟开始,每 5 分钟执行一次,并且秒钟是 10(例如 10:00:10 am, 10:05:10 am 等)

10 0/5 * * * ?

CronTrigger 样例 3 – 每个周三和周五的 10:30, 11:30, 12:30 和 13:30 执行一次

0 30 10-13 ? * WED,FRI

CronTrigger 样例 4 – 每个月 5 日到 20 日,早上 8 点 到 10 点,时钟从 0 开始,每半小时执行一次。注意,不会在上午 10:00 执行,只会在 8:00, 8:30, 9:00 和 9:30 执行。

0 0/30 8-9 5,20 * ?

注意,有的时候使用一个单独的 Trigger 来执行任务将会很复杂,例如“早上 9 点到早上 10 点,每 5 分钟执行一次,下午 1 点到下午 10 点,每 20 分钟执行一次”。这样的话,你可以创建两个 Trigger,并且让两个 Trigger 都关联到同一个 Job。


这里写图片描述

这里写图片描述


创建 CronTrigger

可以使用 TriggerBuilder 来创建 CronTrigger(基于 Trigger 的主要属性),或使用 CronScheduleBuilder 来创建 CronTrigger(基于 CronTrigger 特殊属性)为了使用 DSL 风格,需要进行静态导入:

import static org.quartz.TriggerBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.DateBuilder.*:

创建一个 Trigger,并且在每天的上午 8 点到下午 5 点,每隔 1 分钟执行一次:

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 0/2 8-17 * * ?"))
    .forJob("myJob", "group1")
    .build();

创建一个 Trigger,并且在每天的上午 10:42 执行一次:

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(dailyAtHourAndMinute(10, 42))
    .forJob(myJobKey)
    .build();

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 42 10 * * ?"))
    .forJob(myJobKey)
    .build();

创建一个 Trigger,并在每周三上午 10:42 执行,使用设置的时区而非系统默认时区:

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, 10, 42))
    .forJob(myJobKey)
    .inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
    .build();

trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 42 10 ? * WED"))
    .inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
    .forJob(myJobKey)
    .build();

CronTrigger 错过触发机制

下面的策略将通知 Quartz 如果发生了错过触发将会如何处理(关于错过触发的概念可参看 Triggers 详解)。这些机制以常量形式定义在 CronTrigger 类中(JavaDoc 中详细描述了它们各自的行为)。

MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY
MISFIRE_INSTRUCTION_DO_NOTHING
MISFIRE_INSTRUCTION_FIRE_NOW

所有 Trigger 的默认总是使用 Trigger.MISFIRE_INSTRUCTION_SMART_POLICY (智能策略)。对于 CronTrigger 来说,只能策略就是 MISFIRE_INSTRUCTION_FIRE_NOW。CronTrigger.updateAfterMisfire() 方法的 JavaDoc 详细解释了这个行为的细节。

当创建 CronTrigger 的时候,可以通过 CronSchedulerBuilder 指定错过触发机制:

  trigger = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 0/2 8-17 * * ?")
    .withMisfireHandlingInstructionFireAndProceed())
    .forJob("myJob", "group1")
    .build();

Cron表达式生成器在线网站

http://www.pdtools.net/tools/becron.jsp

http://cron.qqe2.com/

示例

package com.xgj.quartz.quartzItself.cronTriggerDemo;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 
 * 
 * @ClassName: CronTriggerExample
 * 
 * @Description: This Example will demonstrate all of the basics of scheduling
 *               capabilities of Quartz using Cron Triggers.
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年10月7日 下午10:46:05
 */
public class CronTriggerExample {
	public void run() throws Exception {
		Logger log = Logger.getLogger(CronTriggerExample.class);

		log.info("------- Initializing -------------------");

		// First we must get a reference to a scheduler
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();

		log.info("------- Initialization Complete --------");

		log.info("------- Scheduling Jobs ----------------");

		// jobs can be scheduled before sched.start() has been called

		// job 1 will run every 20 seconds
		JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1")
				.build();

		CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1")
				.withSchedule(cronSchedule("0/20 * * * * ?")).build();

		Date ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 2 will run every other minute (at 15 seconds past the minute)
		job = newJob(SimpleJob.class).withIdentity("job2", "group1").build();

		trigger = newTrigger().withIdentity("trigger2", "group1")
				.withSchedule(cronSchedule("15 0/2 * * * ?")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 3 will run every other minute but only between 8am and 5pm
		job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();

		trigger = newTrigger().withIdentity("trigger3", "group1")
				.withSchedule(cronSchedule("0 0/2 8-17 * * ?")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 4 will run every three minutes but only between 5pm and 11pm
		job = newJob(SimpleJob.class).withIdentity("job4", "group1").build();

		trigger = newTrigger().withIdentity("trigger4", "group1")
				.withSchedule(cronSchedule("0 0/3 17-23 * * ?")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 5 will run at 10am on the 1st and 15th days of the month
		job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();

		trigger = newTrigger().withIdentity("trigger5", "group1")
				.withSchedule(cronSchedule("0 0 10am 1,15 * ?")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 6 will run every 30 seconds but only on Weekdays (Monday through
		// Friday)
		job = newJob(SimpleJob.class).withIdentity("job6", "group1").build();

		trigger = newTrigger().withIdentity("trigger6", "group1")
				.withSchedule(cronSchedule("0,30 * * ? * MON-FRI")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		// job 7 will run every 30 seconds but only on Weekends (Saturday and
		// Sunday)
		job = newJob(SimpleJob.class).withIdentity("job7", "group1").build();

		trigger = newTrigger().withIdentity("trigger7", "group1")
				.withSchedule(cronSchedule("0,30 * * ? * SAT,SUN")).build();

		ft = sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " has been scheduled to run at: " + ft
				+ " and repeat based on expression: "
				+ trigger.getCronExpression());

		log.info("------- Starting Scheduler ----------------");

		// All of the jobs have been added to the scheduler, but none of the
		// jobs
		// will run until the scheduler has been started
		sched.start();

		log.info("------- Started Scheduler -----------------");

		log.info("------- Waiting five minutes... ------------");
		try {
			// wait five minutes to show jobs
			Thread.sleep(300L * 1000L);
			// executing...
		} catch (Exception e) {
			//
		}

		log.info("------- Shutting Down ---------------------");

		sched.shutdown(true);

		log.info("------- Shutdown Complete -----------------");

		SchedulerMetaData metaData = sched.getMetaData();
		log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

	}

	public static void main(String[] args) throws Exception {

		CronTriggerExample example = new CronTriggerExample();
		example.run();
	}
}

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */
 
package com.xgj.quartz.quartzItself.cronTriggerDemo;

import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;


/**
 * <p>
 * This is just a simple job that gets fired off many times by example 1
 * </p>
 * 
 */
public class SimpleJob implements Job {

	private static Logger _log = Logger.getLogger(SimpleJob.class);

    /**
     * Quartz requires a public empty constructor so that the
     * scheduler can instantiate the class whenever it needs.
     */
    public SimpleJob() {
    }

    /**
     * <p>
     * Called by the <code>{@link org.quartz.Scheduler}</code> when a
     * <code>{@link org.quartz.Trigger}</code> fires that is associated with
     * the <code>Job</code>.
     * </p>
     * 
     * @throws JobExecutionException
     *             if there is an exception while executing the job.
     */
    public void execute(JobExecutionContext context)
        throws JobExecutionException {

        // This job simply prints out its job name and the
        // date and time that it is running
        JobKey jobKey = context.getJobDetail().getKey();
        _log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
    }

}


示例源码

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值