Quartz-CronTrigger解读

简介: Quartz-CronTrigger解读

20191223202217702.png

概述


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.


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


时间格式


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTEwMTMzOTU3MjY2.png


特殊字符


image.png

image.png

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。


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTEwMTM1NDI5NDM3.png


aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTEwMTM1NTA1NTky.png


创建 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

相关文章
|
SQL Java 关系型数据库
quartz
quartz
127 0
|
设计模式 调度
Quartz-JobListener解读
Quartz-JobListener解读
140 0
|
调度
Quartz-TriggerListener解读
Quartz-TriggerListener解读
69 0
Quartz-SimpleTrigger解读
Quartz-SimpleTrigger解读
84 0
|
API
Quartz-DateBuilder解读
Quartz-DateBuilder解读
106 0
|
存储 调度
Quartz-SchedulerListener解读
Quartz-SchedulerListener解读
146 0
|
Java Linux API
Quartz-Trigger详解
Quartz-Trigger详解
141 0
【Quartz】——CronTrigger 表达式的编写
【Quartz】——CronTrigger 表达式的编写
114 0
|
存储 安全 Java
Quartz 是什么?一文带你入坑
本文主要介绍 Quartz 的使用
699 0
|
Java 调度 Spring
Spring定时任务的实现:Quartz
Spring定时任务的实现:Quartz
171 0
Spring定时任务的实现:Quartz

热门文章

最新文章