Quartz的几个核心的接口和类为:
Job接口:自己写的"定时程序"实现此接口的void execute(JobExecutionContext arg0)方法, Job还有一类为有状态的StatefulJob接口, 如果我们需要在上一个作业执行完后, 根据其执行结果再进行下次作业的执行,则需要实现此接口。
Trigger抽象类:调度类(Scheduler) 在时间到时调用此类,再由trigger类调用指定的定时程序。
Quertz中提供了两类触发器为: SimpleTrigger,CronTrigger。 前者用于实现比较简单的定时功能,例如几点开始,几 点结束,隔多长时间执行,共执行多少次等, 后者提供了使用表达式来描述定时功能, 因此适用于比较复杂的定时描述,例如每个月的最后一个周五, 每周的周四 等。
JobDetail类:具体某个定时程序的详细描述, 包括Name,Group,JobDataMap等。
JobExecutionContext类: 定时程序执行的run-time的上下文环境, 用于得到当前执行的Job的名字,配置的参数等。
JobDataMap类:用于描述一个作业的参数, 参数可以为任何基本类型例如String,float等, 也可为某个对象的引用.
JobListener,TriggerListener接口: 用于监听触发器状态和作业扫行状态,在特写状态执行相应操作。
JobStore类:在哪里执行定进程序,可选的有在内存中, 在数据库中。
简单的定时程序:
时钟调制类:
import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzManage {
Logger log = Logger.getLogger(QuartzManage.class);
public void startup(){
try {
log.debug("-----Initializing Scheduler------------");
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.start();
log.debug("Started Scheduler--------------");
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
实现Job类:
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.print("#################33" + new Date());
try {
Thread.currentThread().sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
执行上面这个类基本实现了一个简单的定时程序。 但问题是现在这个类只能应用在application中, 在web环境里执行还需要添加一些配置,例如添加servlet,添加配置文件quartz. properties或者quartz-job.xml( 在XML文件里以配置方式定义 triiger,定时描述等)。
web应用中使用
时钟调制类:
import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzManage {
Logger log = Logger.getLogger(QuartzManage.class);
public void startup(){
try {
log.debug("-----Initializing Scheduler------------");
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.start();
log.debug("Started Scheduler--------------");
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
实现Job类:
在web.
QuartzInitializer
org.quartz.ee.servlet.
shutdown-on-unload
true
config-file
quartz.properties
2
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
<?xml version="1.0" encoding="UTF-8"?>
<quartz>
<job>
<job-detail>
<name>callJob</name>
<group>group1</group>
<job-class>com.hundsun.u3c.podserver.quartz.job.CallJob</job-class>
</job-detail>
<trigger>
<cron>
<name>cron2</name>
<group>group1</group>
<job-name>callJob</job-name>
<job-group>group1</job-group>
<cron-expression>0/3 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
<job>
<job-detail>
<name>callJob2</name>
<group>group1</group>
<job-class>com.hundsun.u3c.podserver.quartz.job.CallJob2</job-class>
</job-detail>
<trigger>
<cron>
<name>cron3</name>
<group>group2</group>
<job-name>callJob2</job-name>
<job-group>group2</job-group>
<cron-expression>0/10 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
{
public TestJob(){}
{
String name = context.getJobDetail().
System.out.println("job executing..."+name);
}
}
quartz中cronExpression配置说明
字段 |
|
允许值 |
|
允许的特殊字符 |
秒 |
|
0-59 |
|
, - * / |
分 |
|
0-59 |
|
, - * / |
小时 |
|
0-23 |
|
, - * / |
日期 |
|
1-31 |
|
, - * ? / L W C |
月份 |
|
1-12 或者 JAN-DEC |
|
, - * / |
星期 |
|
1-7 或者 SUN-SAT |
|
, - * ? / L C # |
年(可选) |
|
留空, 1970-2099 |
|
, - * / |
表示方式 |
意义 |
"0 0 12 * * ?" |
Fire at 12pm (noon) every day |
"0 15 10 ? * *" |
Fire at 10:15am every day |
"0 15 10 * * ?" |
Fire at 10:15am every day |
"0 15 10 * * ? *" |
Fire at 10:15am every day |
"0 15 10 * * ? 2005" |
Fire at 10:15am every day during the year 2005 |
"0 * 14 * * ?" |
Fire every minute starting at 2pm and ending at 2:59pm, every day |
"0 0/5 14 * * ?" |
Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day |
"0 0/5 14,18 * * ?" |
Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day |
"0 0-5 14 * * ?" |
Fire every minute starting at 2pm and ending at 2:05pm, every day |
"0 10,44 14 ? 3 WED" |
Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. |
"0 15 10 ? * MON-FRI" |
Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday |
"0 15 10 15 * ?" |
Fire at 10:15am on the 15th day of every month |
"0 15 10 L * ?" |
Fire at 10:15am on the last day of every month |
"0 15 10 ? * 6L" |
Fire at 10:15am on the last Friday of every month |
"0 15 10 ? * 6L" |
Fire at 10:15am on the last Friday of every month |
"0 15 10 ? * 6L 2002-2005" |
Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 |
"0 15 10 ? * 6#3" |
Fire at 10:15am on the third Friday of every month |