quartz配置文件详解(转载)

   

quartz学习总结:
一、关于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的值.

public
class HelloJob implements Job {

private static
Log _log = LogFactory.getLog(HelloJob.class);

public
HelloJob() {
 }

public void
execute(JobExecutionContext context)
   throws
JobExecutionException {

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"));
 }

}

二、关于jobdetail:
   
Quartz并不存储一个真正的Job实例,相反的,它通过jobdetail来定义job,并指定job的name和group,在一个调度器
(Scheduler)中,name和group是唯一被定义的,一个触发器(trigger)只能指定一个job,但多个触发器可以指定同一个job.

Scheduler的作用就是调用任务,在指定的时间执行指定的任务。主要方法如下:

scheduleJob方法:

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异常。

deleteJob(String jobName,String groupName)方法:
   
删除指定的job,并且删除所有相关联的触发器。(Delete the identified Job from the
Scheduler - and any associated Triggers.)

四、关于作业存储
   
Quartz提供两种基本作业存储类型。
   
   
第一种类型叫做RAMJobStore,它利用通常的内存来持久化调度程序信息。这种作业存储类型最容易配置、构造和运行。对许多应用来说,这种作业存储
已经足够了。然而,因为调度程序信息是存储在被分配给JVM的内存里面,所以,当应用程序停止运行时,所有调度信息将被丢失。
   
   
第二种类型称为JDBC作业存储。Quartz提供两种不同的实现,但两种实现一般都需要JDBC驱动程序和后台数据库来持久化调度程序信息。这两种类型
的不同在于你是否想要控制数据库事务或这释放控制给应用服务器例如BEA's
WebLogic或Jboss。(这类似于J2EE领域中,Bean管理的事务和和容器管理事务之间的区别)这两种JDBC作业存储是:

·
JobStoreTX:当你想要控制事务或工作在非应用服务器环境中是使用 (注:自己控制事务)。

· JobStoreCMT:当你工作在应用服务器环境中和想要容器控制事务时使用 (web服务器控制事务)。

五、关于触发器

Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。

典型地,如果你需要在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。

另一方面,如果你有许多复杂的作业调度,那么或许需要CronTrigger。

CronTrigger很强大,使用复杂的时间判断来使用,效果很好。

六、关于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 

七、把quartz集成到web应用中
   
1、根据quartz中提供的建表文档,建立数据库.
   
2、把如下quartz.properties文件放置到classes目录下.文件内容如下:
   
 #============================================================================

# Configure Main Scheduler
Properties 
 #============================================================================

#调度器名,无关紧要,名字任意定
 org.quartz.scheduler.instanceName =
ZXScheduler
 org.quartz.scheduler.instanceId = AUTO

#============================================================================

# Configure
ThreadPool   配置数据库连接池
 #============================================================================

org.quartz.threadPool.class =
org.quartz.simpl.SimpleThreadPool
 org.quartz.threadPool.threadCount = 12
 org.quartz.threadPool.threadPriority =
5

#============================================================================

# Configure JobStore 
配置做业存储方式
 #============================================================================

#相当于扫描频率,如果系统基于秒级,应培植成1000,quartz默认为分级(60000)

org.quartz.jobStore.misfireThreshold =
1000

#org.quartz.jobStore.class =
org.quartz.simpl.RAMJobStore

#如果设置内存,就不要设置clusterCheckinInterval等属性

#在这里自己控制事务
 org.quartz.jobStore.class =
org.quartz.impl.jdbcjobstore.JobStoreTX

org.quartz.jobStore.driverDelegateClass =
org.quartz.impl.jdbcjobstore.MSSQLDelegate
 org.quartz.jobStore.useProperties =
false

#配置dataSource名
 org.quartz.jobStore.dataSource = myDS
 #表前缀
 org.quartz.jobStore.tablePrefix = QRTZ_
 org.quartz.jobStore.isClustered =
false

#============================================================================

# Configure Datasources 
配置数据库的连接,不用解释
 #============================================================================

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

3、配置web.xml,启动quartz的初试化类,添加初始化servlet

<servlet>
   
<servlet-name>QuartzInitializer</servlet-name>

<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>

<init-param>
     
<param-name>shutdown-on-unload</param-name>

<param-value>true</param-value>

</init-param>
   
<init-param>
    
<param-name>config-file</param-name>

<param-value>quartz.properties</param-value>

</init-param>
   
<load-on-startup>2</load-on-startup>

</servlet>

4、系统配置完毕。

八、构造cron触发器(个人翻译,英文不好,莫见笑)

cron 触发器规范:

Seconds Minutes Hours Day-of-month Month Day-of-Week Year
   
秒      
分     
时    
天          
月   
周         

Seconds           
0-59    , - *
/
   
Minutes           
0-59    , - *
/
   
Hours             
0-23    , - *
/
   
Day-of-month      
1-31    , - * ?
/ L C
   
Month             
1-12 or
JAN-DEC    , - *
/
   
Day-of-Week       
1-7 or
SUN-SAT    , - *
? / L C #
    Year
(Optional)   
empty,
1970-2099    , -
* /

关于字符串的设置(在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 ranges For example "10-12" in the hour
field means "the hours 10, 11 and 12".
   
'-'字符被用来设置范围,比如"10-12"在小时字段的意义为"10点,11点,12点"

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"的意义即为:在周一、周三、周五激活

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 '/'.

'/'字符被用来设置增量。例如秒字段设置"0/15"的意思为从0开始,每15秒触发,即在0、15、30、45秒触发,秒字段设置"5/15"的意思
为,从5开始,第15秒触发,即在5、20、35、50秒触发.你还可以在'*'后面使用'/'字符,在这种情况下'*'与字符'0'意义相同。

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 the features described for the 'C' character is not
complete
   
'C'被用在天和周字段中,'C'是'calendar'的缩写.(不太明白,关于日历的支持还不完善)

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!
   
要注意'?'和'*'在周和天字段带来的影响。
   
注意以下例子:

1、"0 15 10 * * 6L 2002-2005"  
在2002至2005年的每月每天的10:15触发。
    2、"0 15 10 ?
* 6L 2002-2005"  
在2002至2005年的每月的最后一个周五触发。
   
1中*表示每天,覆盖了6L(最后一个周五)

最新文章

  1. ajax 同步和异步
  2. cmd 常用指令
  3. div 自动满屏
  4. PHP PC端接入支付宝和微信感悟
  5. zoj1665 dij变形
  6. Oracle 11g必须开启的服务及服务详细介绍(转)
  7. html5实现饼图和线图-我们到底能走多远系列(34)
  8. 第三章 jQuery中的DOM操作
  9. 【Oracle】删除重复记录
  10. CentOS7 安装JDK
  11. Android View系统解析(上)
  12. jquery treeTable插件使用细则
  13. SQL Server使用导入导出向导导入超过4000个字符的字段的数据
  14. Akka Serialization
  15. [daily] 如何用emacs+xcscope阅读内核源码
  16. scrapy--分布式爬虫
  17. 如何计算 App 的启动时间
  18. 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
  19. javapms部署之后首页不能正常显示问题
  20. 【转载】python-协程

热门文章

  1. ubuntu下载工具uget和aria2
  2. QEMU/KVM虚拟机安装配置
  3. 线程池 Threadpool (还需要补充)
  4. 第八章 交互技术,8.1 VR电商购物(作者:宋五)
  5. [Java] ApplicationContext 辅助类
  6. MSSQL部分补丁的列表及下载地址(持续更新)
  7. 海拔高度图*.dem文件的读取—vtkDEMReader
  8. 数据量大的数据转换成jason并显示在页面上
  9. D3.js学习(五)
  10. MVC中渲染页面