quartz in practice

基于quartz 2.2.1版本,quartz使用数据库(mysql)作为持久化策略。

配置quartz.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# quartz配置 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/index.html
# 主要配置 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigMain.html
org.quartz.scheduler.instanceName = DataptQuartzScheduler
# 线程池配置 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigThreadPool.html
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
# 持久化配置 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigJobStoreTX.html
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = dataptDS
# 数据库连接设置 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigDataSources.html
org.quartz.dataSource.dataptDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.dataptDS.URL = jdbc:mysql://ip:port/quartz?useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.dataptDS.user = username
org.quartz.dataSource.dataptDS.password = password
org.quartz.dataSource.dataptDS.maxConnections = 30

初始化mysql

可以适用多种数据库,初始化sql文件在docs/dbTables/下,mysql可以使用tables_mysql_innodb.sql,官方建议使用innodb引擎。
我这边数据库名使用quartz.

Listener

JobListener和TriggerListener执行顺序:

  1. trigger fired
  2. job to be executed
  3. job执行
  4. job was executed
  5. trigger complete

JobListener

参考 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/cookbook/JobListeners.html

TriggerListener

参考 http://www.quartz-scheduler.org/documentation/quartz-2.2.x/cookbook/TriggerListeners.html

参考