quartz学习总结:
1、关于job:
用Quartz的行话讲,做业是一个执行任务的简单Java类。任务能够是任何Java代码。只需你实现org.quartz.Job接口而且在出现严重 错误状况下抛出JobExecutionException异常便可。Job接口包含惟一的一个方法execute(),做业从这里开始执行。一旦实现了 Job接口和execute()方法,当Quartz肯定该是做业运行的时候,它将调用你的做业。Execute()方法内就彻底是你要作的事情。要注 意,本身实现job时必须有一个public 的无参数的构造方法.对于job,大多数状况下都要依赖于某些具体的条件,这时,就要用到JobDataMap了。JobDataMap是Map的一个子 类,获取时很简单,直接用get方法就ok了,基于参数,咱们就能够定制不一样的job任务了。下面是一个简单的job,用来列举出全部的参数而且得到参数 名为name的值.html
public class HelloJob implements Job {web
private static Log _log = LogFactory.getLog(HelloJob.class);sql
public HelloJob() {
}数据库
public void execute(JobExecutionContext context)
throws JobExecutionException {express
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();服务器
_log.info("要执行的参数以下:");
Iterator i = jobDataMap.entrySet().iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
_log.info(me.getKey() + ": "+me.getValue());
}
_log.info("U Are Welcome:"+jobDataMap.get("name"));
}框架
}ide
2、关于jobdetail:
Quartz并不存储一个真正的Job实例,相反的,它经过jobdetail来定义job,并指定job的name和group,在一个调度器 (Scheduler)中,name和group是惟一被定义的,一个触发器(trigger)只能指定一个job,但多个触发器能够指定同一个job.sqlserver
Scheduler的做用就是调用任务,在指定的时间执行指定的任务。主要方法以下:学习
scheduleJob(JobDetail jobDetail, Trigger trigger):把jobDetail添加到调度器中,并指定触发器trigger.在这里要注意,在同一个调度器中,jobDetail的name和 group是惟一的;Trigger的name和group也必须是惟一的。若是在trigger中指定job的name,则该name必须和 jobDetail的name保持一致,不然会抛出异常。
scheduleJob(Trigger trigger):指定的trigger中必须包含jobdetai的name.以便于让quartz知道要执行的任务,若是指定的jobdetail的 name不在调度器中的任务列表中,则会抛出JobPersistenceException异常。
4、关于做业存储
Quartz提供两种基本做业存储类型。
第一种类型叫作RAMJobStore,它利用一般的内存来持久化调度程序信息。这种做业存储类型最容易配置、构造和运行。对许多应用来讲,这种做业存储 已经足够了。然而,由于调度程序信息是存储在被分配给JVM的内存里面,因此,当应用程序中止运行时,全部调度信息将被丢失。
第二种类型称为JDBC做业存储。Quartz提供两种不一样的实现,但两种实现通常都须要JDBC驱动程序和后台数据库来持久化调度程序信息。这两种类型 的不一样在于你是否想要控制数据库事务或这释放控制给应用服务器例如BEA's WebLogic或Jboss。(这相似于J2EE领域中,Bean管理的事务和和容器管理事务之间的区别)这两种JDBC做业存储是:
· JobStoreTX:当你想要控制事务或工做在非应用服务器环境中是使用 (注:本身控制事务)。
· JobStoreCMT:当你工做在应用服务器环境中和想要容器控制事务时使用 (web服务器控制事务)。
Quartz中的触发器用来告诉调度程序做业何时触发。框架提供了一把触发器类型,但两个最经常使用的是SimpleTrigger和CronTrigger。SimpleTrigger为须要简单打火调度而设计。
6、关于Quartz中的几个表:
QRTZ_TRIGGERS 存放Trigger(包括SIMPLE_TRIGGERS和CRON_TRIGGERS)和jobDetail的对应关系
QRTZ_TRIGGER_LISTENERS
QRTZ_SIMPLE_TRIGGERS 存储简单触发器
QRTZ_SCHEDULER_STATE
QRTZ_PAUSED_TRIGGER_GRPS
QRTZ_LOCKS
QRTZ_JOB_LISTENERS
QRTZ_JOB_DETAILS 存储jobDetail
QRTZ_FIRED_TRIGGERS
QRTZ_CRON_TRIGGERS 存储复杂触发器CRON_TRIGGERS
QRTZ_CALENDARS
QRTZ_BLOB_TRIGGERS
7、把quartz集成到web应用中
一、根据quartz中提供的建表文档,创建数据库.
二、把以下quartz.properties文件放置到classes目录下.文件内容以下:
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
#调度器名,可有可无,名字任意定
org.quartz.scheduler.instanceName = ZXScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 12
org.quartz.threadPool.threadPriority = 5
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#若是设置内存,就不要设置clusterCheckinInterval等属性
#在这里本身控制事务
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#配置dataSource名
org.quartz.jobStore.dataSource = myDS
#表前缀
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.dataSource.myDS.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=quartzTest
org.quartz.dataSource.myDS.user = sa
org.quartz.dataSource.myDS.password = sa
org.quartz.dataSource.myDS.maxConnections = 5
三、配置web.xml,启动quartz的初试化类,添加初始化servlet
8、构造cron触发器(我的翻译,英文很差,莫见笑)
cron 触发器规范:
Seconds Minutes Hours Day-of-month Month Day-of-Week Year
秒 分 时 天 月 周 年
关于字符串的设置(在cron expression中全部字符不区分大小写):
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
"*"字符被用来指定全部的值,例如,"*"在分钟字段时表示每一分钟
The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
"?"字符在天和周字段中使用。表示没有指定值,天和周字段指定一个,但不能两个都指定为"?"
The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
','字符被用来设置添加的值,例如在周字段设置"MON,WED,FRI"的意义即为:在周1、周3、周五激活
The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". You can also specify '/' after the '*' character - in this case '*' is equivalent to having '0' before the '/'.
The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.
'#'被用在周字段。它用来指定第几个周几中激活。如:"6#3"-->月的第三个周五;"2#1"-->月的第一个周一;"4#5"-- >月的第五个周三。要注意,若是要使用#后面跟5,但当月并无第五周相应的周天,那么job将不被执行(激活);
The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".
Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
同时在周、天中使用'?'还不完善,目前只在二者中使用一个。
Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!
要注意'?'和'*'在周和天字段带来的影响。
注意如下例子:
一、"0 15 10 * * 6L 2002-2005" 在2002至2005年的每个月天天的10:15触发。 二、"0 15 10 ? * 6L 2002-2005" 在2002至2005年的每个月的最后一个周五触发。 1中*表示天天,覆盖了6L(最后一个周五)