|
一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素。从左至右,这些元素的定义如下:
1.秒(0–59)
2.分钟(0–59)
3.小时(0–23)
4.月份中的日期(1–31)
5.月份(1–12或JAN–DEC)
6.星期中的日期(1–7或SUN–SAT)
7.年份(1970–2099)
每一个元素都可以显式地规定一个值(如6),一个区间(如9-12),一个列表(如9,11,13)或一个通配符(如*)。“月份中的日期”和“星期中的日期”这两个元素是互斥的,因此应该通过设置一个问号(?)来表明你不想设置的那个字段。表中显示了一些cron表达式的例子和它们的意义:
Cron 表达式包括以下 7 个字段:
一些cron表达式的例子
表 达 式
|
意 义
|
0 0 10,14,16 * * ?
|
每天上午10点,下午2点和下午4点
|
0 0,15,30,45 * 1-10 * ?
|
每月前10天每隔15分钟
|
30 0 0 1 1 ? 2012
|
在2012年1月1日午夜过30秒时
|
0 0 8-5 ? * MON-FRI
|
每个工作日的工作时间
|
对于cronReportTrigger,我们设置cronExpression为0 0 6 * * ?可以把它读作“在任何月份任何日期(不管是星期几)的6时0分0秒执行触发器。换句话说,这个触发器会在每天早晨6:00执行。
使用CronTriggerBean完全能够满足课程主任的期望了。现在剩下要做的只是启动这个工作了。
- 秒
- 分
- 小时
- 月内日期
- 月
- 周内日期
- 年(可选字段)
特殊字符
Cron 触发器利用一系列特殊字符,如下所示:
- 反斜线(/)字符表示增量值。例如,在秒字段中“5/15”代表从第 5 秒开始,每 15 秒一次。
- 问号(?)字符和字母 L 字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入“?”,表示周内日期值无关紧要。字母 L 字符是 last 的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果“L”单独存在,就等于“7”,否则代表当月内周内日期的最后一个实例。所以“0L”表示安排在当月的最后一个星期日执行。
- 在月内日期字段中的字母(W)字符把执行安排在最靠近指定值的工作日。把“1W”放在月内日期字段中,表示把执行安排在当月的第一个工作日内。
- 井号(#)字符为给定月份指定具体的工作日实例。把“MON#2”放在周内日期字段中,表示把任务安排在当月的第二个星期一。
- 星号(*)字符是通配字符,表示该字段可以接受任何可能的值。
SimpleJob.java
/*
* Copyright 2005 OpenSymphony
*
* 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 org.quartz.examples.example3;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* <p>
* This is just a simple job that gets fired off many times by example 1
* </p>
*
* @author Bill Kratzer
*/
public class SimpleJob implements Job {
private static Log _log = LogFactory.getLog(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
String jobName = context.getJobDetail().getFullName();
_log.info("SimpleJob says: " + jobName + " executing at " + new Date());
System.out.println(context.getTrigger().getFullName());
}
}
CronTriggerExample.java
/*
* Copyright 2005 OpenSymphony
*
* 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 org.quartz.examples.example3;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
/**
* This Example will demonstrate all of the basics of scheduling capabilities of
* Quartz using Cron Triggers.
*
* @author Bill Kratzer
*/
public class CronTriggerExample {
public void run() throws Exception {
Log log = LogFactory.getLog(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 = new JobDetail("job1", "group1", SimpleJob.class);
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1",
"group1", "0/20 * * * * ?");
sched.addJob(job, true);
Date ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job2", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger2", "group1", "job2", "group1",
"15 0/2 * * * ?");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job3", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger3", "group1", "job3", "group1",
"0 0/2 8-17 * * ?");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job4", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger4", "group1", "job4", "group1",
"0 0/3 17-23 * * ?");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job5", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger5", "group1", "job5", "group1",
"0 0 10am 1,15 * ?");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job6", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger6", "group1", "job6", "group1",
"0,30 * * ? * MON-FRI");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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 = new JobDetail("job7", "group1", SimpleJob.class);
trigger = new CronTrigger("trigger7", "group1", "job7", "group1",
"0,30 * * ? * SAT,SUN");
sched.addJob(job, true);
ft = sched.scheduleJob(trigger);
log.info(job.getFullName() + " 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.numJobsExecuted() + " jobs.");
}
public static void main(String[] args) throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
}
}
运行结果:
2008-12-22 21:25:22 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Initializing -------------------
2008-12-22 21:25:23 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
2008-12-22 21:25:23 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2008-12-22 21:25:23 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.1.6.4 created.
2008-12-22 21:25:23 org.quartz.simpl.RAMJobStore initialize
信息: RAMJobStore initialized.
2008-12-22 21:25:23 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2008-12-22 21:25:23 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 1.6.4
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Initialization Complete --------
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Scheduling Jobs ----------------
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job1 has been scheduled to run at: Mon Dec 22 21:25:40 CST 2008 and repeat based on expression: 0/20 * * * * ?
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job2 has been scheduled to run at: Mon Dec 22 21:26:15 CST 2008 and repeat based on expression: 15 0/2 * * * ?
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job3 has been scheduled to run at: Tue Dec 23 08:00:00 CST 2008 and repeat based on expression: 0 0/2 8-17 * * ?
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job4 has been scheduled to run at: Mon Dec 22 21:27:00 CST 2008 and repeat based on expression: 0 0/3 17-23 * * ?
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job5 has been scheduled to run at: Thu Jan 01 10:00:00 CST 2009 and repeat based on expression: 0 0 10AM 1,15 * ?
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job6 has been scheduled to run at: Mon Dec 22 21:25:30 CST 2008 and repeat based on expression: 0,30 * * ? * MON-FRI
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: group1.job7 has been scheduled to run at: Sat Dec 27 00:00:00 CST 2008 and repeat based on expression: 0,30 * * ? * SAT,SUN
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Starting Scheduler ----------------
2008-12-22 21:25:23 org.quartz.core.QuartzScheduler start
信息: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Started Scheduler -----------------
2008-12-22 21:25:23 org.quartz.examples.example3.CronTriggerExample run
信息: ------- Waiting five minutes... ------------
group1.trigger6
2008-12-22 21:25:30 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job6 executing at Mon Dec 22 21:25:30 CST 2008
group1.trigger1
2008-12-22 21:25:40 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job1 executing at Mon Dec 22 21:25:40 CST 2008
group1.trigger1
2008-12-22 21:26:00 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job1 executing at Mon Dec 22 21:26:00 CST 2008
group1.trigger6
2008-12-22 21:26:00 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job6 executing at Mon Dec 22 21:26:00 CST 2008
group1.trigger2
2008-12-22 21:26:15 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job2 executing at Mon Dec 22 21:26:15 CST 2008
group1.trigger1
2008-12-22 21:26:20 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job1 executing at Mon Dec 22 21:26:20 CST 2008
group1.trigger6
2008-12-22 21:26:30 org.quartz.examples.example3.SimpleJob execute
信息: SimpleJob says: group1.job6 executing at Mon Dec 22 21:26:30 CST 2008
|