cron总结

cron详解参见:《quartz CronExpression表达式

CronTrigger配置完整格式为7个: [秒]   [分]   [小时]   [日]   [月]   [周]   [年]   ,一般为6个,省去最后那个年。举例:"0/20 * * * * ?"    每20秒执行一次,无限期重复

序号含义默认默认值含义允许填写的值允许的通配符 通配符举例
1*每秒0-59, - * /0/20 每20秒
2*每分0-59, - * / 10,20,30 第10、20、30分钟
3小时*每小时0-23, - * / 8-17 上午8点到下午17点
4*每日1-31, - * ? / L W

1,15 月的1号和15号

6L 表示这个月的倒数第6天

"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。

5*每月1-12 or JAN-DEC, - * /

2,4,6  2月4月和6月

JAN-DEC  1月到12月

6每周1-7 or SUN-SAT, - * ? / L # SUN-SAT  星期天到星期六
7非必填 空 或 1970-2099, - * / 

任务类

 package org.quartz.examples.example3;

 import java.util.Date;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey; /**
* 任务job
*/
public class SimpleJob implements Job { private static Logger LOG = LoggerFactory.getLogger(SimpleJob.class); // 必须要有public修饰的无参构造函数
public SimpleJob() {
} // 任务执行方法
public void execute(JobExecutionContext context) throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey(); //jobKey长这样:group1.job6
LOG.info("执行任务。SimpleJob says: " + jobKey + " executing at " + new Date());
} }

调度管理类

 package org.quartz.examples.example3;

 import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger; 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.Date; /**
* 这个示例将演示使用Cron触发器的Quartz的所有基本调度功能。
*
*/
public class CronTriggerExample {
Logger LOG = LoggerFactory.getLogger(CronTriggerExample.class); /*
* 以下用到的cron总结: "0/20 * * * * ?" 每20秒执行一次
*/
public void run() throws Exception {
// 初始化一个调度工厂,并实例化一个调度类
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler(); JobDetail job = null;
CronTrigger trigger = null;
Date ft = null; // ========================================================
// ============ job1 每20秒执行一次,无限期重复
// ========================================================
job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();
ft = sched.scheduleJob(job, trigger);
LOG.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: "
+ trigger.getCronExpression()); // ========================================================
// ============ job2 将每隔2分钟运行一次(每分钟过后15秒)
// ========================================================
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()); // ========================================================
// ============ job3 将每隔一分钟运行一次,但只在上午8点到下午5点之间才运行
// ========================================================
job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();
trigger = newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule("0 0/1 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()); // ========================================================
// ============ job4 每三分钟运行一次,但只在下午5点到11点之间运行
// ========================================================
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()); // ========================================================
// ============ job5将在每月的第1天和第15天上午10点运行
// ========================================================
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()); // ========================================================
// ============ job6 将每30秒运行一次,但仅限于工作日(周一至周五)
// ========================================================
// 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()); // ========================================================
// ============ job7 每30秒运行一次,但只在周末(周六和周日)
// ========================================================
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()); // ====================================================================
// ============ 启动调度器
// ====================================================================
sched.start(); try {
Thread.sleep(300L * 1000L); // 等待5分钟
} catch (Exception e) {
}

        /**
        * 终止调度。
        * 但是如果当前仍然有任务正在运行中,则会等待该任务执行完毕再终止,期间debug信息会打印:
        * 21:48:54.965 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Waiting for thread TestScheduler_Worker-1 to shut down
        */

         sched.shutdown(true);
// 显示调度器的一些统计信息
SchedulerMetaData metaData = sched.getMetaData();
LOG.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
} public static void main(String[] args) throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
} }
05-27 21:04