- 浏览: 984895 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Quartz储存方式之JDBC JobStoreTX:http://blog.csdn.net/uhzgnaw/article/details/46358333
Quartz的任务方式有两种,一种是RAM但这种方式,应用中断或重启任务易丢失,而RDB方式将任务执行情况及上下文保存到数据库,中断或重启可以重新执行。首先下载Quazrt发布包,解压,运行doc文件夹下(E:\Quartz\quartz-2.2.3\docs\dbTables)的tables_mysql_innodb.sql脚本,如果用Quartz的默认数据连接池,则需要夹C3po相关的依赖包,为什么是c3p0,可以查看一下这篇文章:
Quartz 任务存储JobStoreTX 持久化之RDB-源码分析:http://donald-draper.iteye.com/blog/2323409
新建JOb
Job监听器:
CronTrigger触发器测试:
运行程序控制台输出:
九月 08, 2016 2:32:52 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: MLog clients using slf4j logging.
九月 08, 2016 2:32:52 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Using default implementation for ThreadExecutor
九月 08, 2016 2:32:53 下午 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
九月 08, 2016 2:32:53 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.2.2.1 created.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport initialize
信息: Using thread monitor-based data access locking (synchronization).
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreTX initialize
信息: JobStoreTX initialized.
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler initialize
信息: Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 5 threads.
Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 2.2.1
九月 08, 2016 2:32:53 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2sigdn9j2cw7cdnlmynx|529b9e76, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 2sigdn9j2cw7cdnlmynx|529b9e76, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 120, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Freed 0 triggers from 'acquired' / 'blocked' state.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovering 0 jobs that were in-progress at the time of the last shut-down.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovery complete.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 'complete' triggers.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 stale fired job entries.
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED started.
九月 08, 2016 2:32:55 下午 job.SimpleJob execute
//第一次
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:32:55 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:32:55 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:32:55,计数: 1
九月 08, 2016 2:32:55 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:32:55
九月 08, 2016 2:32:55 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
中断程序
查看数据库job_details,cron_trigger,fire_trriger表,如下
mysql> use test;
Database changed
mysql> select * from qrtz_job_details;
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SCHED_NAME | JOB_NAME | JOB_GROUP | DESCRIPTION | JOB_CLASS_NAME | IS_DURABLE | IS_NONCONCURRENT | IS_UPDATE_DATA | REQUESTS_RECOVERY | JOB_DATA |
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| QuartzScheduler | SimpleJob | Job-Group-Simple | NULL | job.SimpleJob | 0 | 0 | 0 | 0 |
?
loadFactorI
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
mysql> select * from qrtz_cron_triggers;
+-----------------+---------------+----------------------+-----------------+---------------+
| SCHED_NAME | TRIGGER_NAME | TRIGGER_GROUP | CRON_EXPRESSION | TIME_ZONE_ID |
+-----------------+---------------+----------------------+-----------------+---------------+
| QuartzScheduler | SimpleTrigger | Trriger-Group-Simple | 0/5 * * * * ? | Asia/Shanghai |
+-----------------+---------------+----------------------+-----------------+---------------+
1 row in set
mysql> select * from qrtz_fired_triggers;
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| SCHED_NAME | ENTRY_ID | TRIGGER_NAME | TRIGGER_GROUP | INSTANCE_NAME | FIRED_TIME | SCHED_TIME | PRIORITY | STATE | JOB_NAME | JOB_GROUP | IS_NONCONCURRENT | REQUESTS_RECOVERY |
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| QuartzScheduler | NON_CLUSTERED1473317339356 | SimpleTrigger | Trriger-Group-Simple | NON_CLUSTERED | 1473317340303 | 1473317345000 | 5 | ACQUIRED | NULL | NULL | 0 | 0 |
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
1 row in set
可以看到Quart将任务信息放在了job,trriger中,触发的任务放在fire_trriger中
屏蔽一下代码,重新启动程序
可以看到一下控制台输出
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: MLog clients using slf4j logging.
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Using default implementation for ThreadExecutor
九月 08, 2016 2:33:27 下午 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
九月 08, 2016 2:33:27 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
九月 08, 2016 2:33:27 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.2.2.1 created.
九月 08, 2016 2:33:27 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport initialize
信息: Using thread monitor-based data access locking (synchronization).
九月 08, 2016 2:33:27 下午 org.quartz.impl.jdbcjobstore.JobStoreTX initialize
信息: JobStoreTX initialized.
九月 08, 2016 2:33:27 下午 org.quartz.core.QuartzScheduler initialize
信息: Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 5 threads.
Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 2.2.1
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2sigdn9j2cwy271yl9aea|70e98812, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 2sigdn9j2cwy271yl9aea|70e98812, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 120, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Freed 1 triggers from 'acquired' / 'blocked' state.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovering 0 jobs that were in-progress at the time of the last shut-down.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovery complete.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 'complete' triggers.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 1 stale fired job entries.
九月 08, 2016 2:33:28 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED started.
//第一次
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 1
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
//第二次
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 2
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
//第三次
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 3
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
警告: 已经暂停了触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
警告: 暂停当前线程5秒钟后在恢复触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
警告: 正在恢复了触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
//第四次
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:33 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:33,计数: 4
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:33
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
严重: 正在停止并且移除触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler shutdown
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED shutting down.
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler standby
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED paused.
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler shutdown
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED shutdown complete.
quartz.properties文件:
#线程池实现类
org.quartz.threadPool.class =org.quartz.simpl.SimpleThreadPool
#线程池数
org.quartz.threadPool.threadCount = 5
#线程池优先级
org.quartz.threadPool.threadPriority = 5
#线程池运行模式守后,还是前台
org.quartz.threadPool.makeThreadsDaemons=false
###线程池配置的继承属性
org.quartz.threadPool.threadsInheritGroupOfInitializingThread=true
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
###保存job调度执行信息到RAM,这种方式当应用下次重启时,job调度信息容易丢失
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#org.quartz.jobStore.misfireThreshold = 60000
############.JobStoreTX,是将job调度信息保存到RDB中,应用重启时可以重新调度job
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=test
#org.quartz.dataSource.test.connectionProvider.class=org.quartz.utils.PoolingConnectionProvider
org.quartz.dataSource.test.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.test.URL=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=utf8&characterSetResults\=utf8
org.quartz.dataSource.test.user=root
org.quartz.dataSource.test.password=123456
org.quartz.dataSource.test.maxConnections=20
总结:
从以上分析可以看出,JobStoreTX-RDB方式,实际上,就是将job存储在 qrtz_job_details表中,将cron_triggers存储在qrtz_cron_triggers,而触发的任务存储在qrtz_fired_triggers表中,应用崩溃时,而已触发的任务保存在qrtz_fired_triggers,下次应用启动时,直接调度qrtz_fired_triggers表中的任务。
备注:其他相关表
qrtz_paused_trigger_grps:暂定触发任务表
qrtz_triggers:触发任务表
qrtz_simple_triggers:简单触发器表
qrtz_scheduler_state:调度状态表
来看一下qrtz_triggers:
mysql> select * from qrtz_triggers;
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
| SCHED_NAME | TRIGGER_NAME | TRIGGER_GROUP | JOB_NAME | JOB_GROUP | DESCRIPTION | NEXT_FIRE_TIME | PREV_FIRE_TIME | PRIORITY | TRIGGER_STATE | TRIGGER_TYPE | START_TIME | END_TIME | CALENDAR_NAME | MISFIRE_INSTR | JOB_DATA |
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
| QuartzScheduler | SimpleTrigger | Trriger-Group-Simple | SimpleJob | Job-Group-Simple | NULL | 1473317345000 | 1473317340000 | 5 | ACQUIRED | CRON | 1473317339000 | 0 | NULL | 0 | |
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
1 row in set
Quartz的任务方式有两种,一种是RAM但这种方式,应用中断或重启任务易丢失,而RDB方式将任务执行情况及上下文保存到数据库,中断或重启可以重新执行。首先下载Quazrt发布包,解压,运行doc文件夹下(E:\Quartz\quartz-2.2.3\docs\dbTables)的tables_mysql_innodb.sql脚本,如果用Quartz的默认数据连接池,则需要夹C3po相关的依赖包,为什么是c3p0,可以查看一下这篇文章:
Quartz 任务存储JobStoreTX 持久化之RDB-源码分析:http://donald-draper.iteye.com/blog/2323409
新建JOb
package job; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SimpleJob implements Job { private static int jobCount = 0; Logger log = LoggerFactory.getLogger(SimpleJob.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 此任务仅打印日志便于调试、观察 log.info("==================:"+this.getClass().getName() + " was just triggered..."); JobKey jobKey = context.getJobDetail().getKey(); TriggerKey triggerKey = context.getTrigger().getKey(); Scheduler scheduler = context.getScheduler(); //启动线程监听触发器的状态 new Thread(new SchedEventLister(triggerKey,scheduler)).start(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); log.info("任务分组名称: " + jobKey.getGroup()+",任务名称: " + jobKey.getName()+",触发器组名称: " + triggerKey.getGroup()+ ",触发器名称: "+ triggerKey.getName()+" 执行时间: " + sdf.format(new Date()) + ",计数: " + (++jobCount)) ; //如果任务执行超过15次就结束它 /* if(jobCount > 3){ log.warn("结束任务..."); try { //Delete the identified Job from the Scheduler - and any associated Triggers. //从调度器中删除这个唯一任务时同时会删除相关联的触发器^_^ scheduler.deleteJob(jobKey); } catch (SchedulerException e) { e.printStackTrace(); } }*/ if(jobCount == 2 ){ //暂停触发器 try { scheduler.pauseTrigger(triggerKey); log.warn("已经暂停了触发器...,触发器组名: " + triggerKey.getGroup() + ",触发器名称: " + triggerKey.getName()); } catch (SchedulerException e) { throw new RuntimeException(e); } } if(jobCount == 4){ try { scheduler.pauseTrigger(triggerKey); // 停止触发器 scheduler.unscheduleJob(triggerKey);//移除触发器 scheduler.pauseJob(jobKey);// 停止任务 scheduler.deleteJob(jobKey);// 删除任务 log.error("正在停止并且移除触发器...,触发器组名: " + triggerKey.getGroup() + ",触发器名称: " + triggerKey.getName()); } catch (SchedulerException e) { throw new RuntimeException(e); } } } }
Job监听器:
package job; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SchedEventLister implements Runnable{ private static Logger log = LoggerFactory.getLogger(SchedEventLister.class); private Scheduler sched = null; private TriggerKey triggerKey = null; public SchedEventLister(TriggerKey triggerKey,Scheduler sched) { log.info("触发器线程监听启动...."); this.triggerKey = triggerKey; this.sched = sched; } @Override public void run() { log.info("触发器线程监听中。。。。" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") .format(new Date())); // 获取当前触发器的状态 Trigger.TriggerState ts = null; try { ts = sched.getTriggerState(triggerKey); } catch (SchedulerException e) { e.printStackTrace(); } // 如果为暂停状态就恢复 if (ts == Trigger.TriggerState.PAUSED) { // 暂停10秒在恢复 try { log.warn("暂停当前线程5秒钟后在恢复触发器...,触发器组名: " + triggerKey.getGroup() + ",触发器名称: " + triggerKey.getName()); Thread.sleep(5L * 1000L); } catch (InterruptedException e1) { e1.printStackTrace(); } // 恢复触发器 log.warn("正在恢复了触发器...,触发器组名: " + triggerKey.getGroup() + ",触发器名称: " + triggerKey.getName()); try { sched.resumeTrigger(triggerKey); } catch (SchedulerException e) { throw new RuntimeException(e); } } else if (ts == Trigger.TriggerState.NORMAL) { log.info("触发器线程监听中。。。。,状态:正常 "); } else if (ts == Trigger.TriggerState.NONE) { log.info("触发器线程监听中。。。。,状态:没有触发器 "); } else if (ts == Trigger.TriggerState.ERROR) { log.info("触发器线程监听中。。。。,状态:错误 "); } else if (ts == Trigger.TriggerState.BLOCKED) { log.info("触发器线程监听中。。。。,状态:堵塞 "); } } }
CronTrigger触发器测试:
package job; import java.util.concurrent.TimeUnit; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestCronTrriger { private static Logger logger = LoggerFactory.getLogger(testSimpleTrriger.class); public static void main(String[] args) { try { // 获取Scheduler实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler cronScheduler = schedulerFactory.getScheduler(); // 具体任务 JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("SimpleJob", "Job-Group-Simple").build(); // 触发时间点 // 交由Scheduler安排触发 String cronExpression = "0/5 * * * * ?"; // 每分钟的30s起,每5s触发任务 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("SimpleTrigger", "Trriger-Group-Simple") .withSchedule(cronScheduleBuilder) .build(); cronScheduler.scheduleJob(job, cronTrigger); cronScheduler.start(); try { /* 为观察程序运行,此设置主程序睡眠2分钟才继续往下运行(因下一个步骤是“关闭Scheduler”) */ TimeUnit.MINUTES.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } // 关闭Scheduler cronScheduler.shutdown(); } catch (SchedulerException se) { logger.error(se.getMessage(), se); } } }
运行程序控制台输出:
九月 08, 2016 2:32:52 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: MLog clients using slf4j logging.
九月 08, 2016 2:32:52 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Using default implementation for ThreadExecutor
九月 08, 2016 2:32:53 下午 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
九月 08, 2016 2:32:53 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.2.2.1 created.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport initialize
信息: Using thread monitor-based data access locking (synchronization).
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreTX initialize
信息: JobStoreTX initialized.
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler initialize
信息: Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 5 threads.
Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
九月 08, 2016 2:32:53 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 2.2.1
九月 08, 2016 2:32:53 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2sigdn9j2cw7cdnlmynx|529b9e76, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 2sigdn9j2cw7cdnlmynx|529b9e76, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 120, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Freed 0 triggers from 'acquired' / 'blocked' state.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovering 0 jobs that were in-progress at the time of the last shut-down.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovery complete.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 'complete' triggers.
九月 08, 2016 2:32:53 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 stale fired job entries.
九月 08, 2016 2:32:53 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED started.
九月 08, 2016 2:32:55 下午 job.SimpleJob execute
//第一次
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:32:55 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:32:55 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:32:55,计数: 1
九月 08, 2016 2:32:55 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:32:55
九月 08, 2016 2:32:55 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
中断程序
查看数据库job_details,cron_trigger,fire_trriger表,如下
mysql> use test;
Database changed
mysql> select * from qrtz_job_details;
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SCHED_NAME | JOB_NAME | JOB_GROUP | DESCRIPTION | JOB_CLASS_NAME | IS_DURABLE | IS_NONCONCURRENT | IS_UPDATE_DATA | REQUESTS_RECOVERY | JOB_DATA |
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| QuartzScheduler | SimpleJob | Job-Group-Simple | NULL | job.SimpleJob | 0 | 0 | 0 | 0 |
?
loadFactorI
+-----------------+-----------+------------------+-------------+----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
mysql> select * from qrtz_cron_triggers;
+-----------------+---------------+----------------------+-----------------+---------------+
| SCHED_NAME | TRIGGER_NAME | TRIGGER_GROUP | CRON_EXPRESSION | TIME_ZONE_ID |
+-----------------+---------------+----------------------+-----------------+---------------+
| QuartzScheduler | SimpleTrigger | Trriger-Group-Simple | 0/5 * * * * ? | Asia/Shanghai |
+-----------------+---------------+----------------------+-----------------+---------------+
1 row in set
mysql> select * from qrtz_fired_triggers;
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| SCHED_NAME | ENTRY_ID | TRIGGER_NAME | TRIGGER_GROUP | INSTANCE_NAME | FIRED_TIME | SCHED_TIME | PRIORITY | STATE | JOB_NAME | JOB_GROUP | IS_NONCONCURRENT | REQUESTS_RECOVERY |
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| QuartzScheduler | NON_CLUSTERED1473317339356 | SimpleTrigger | Trriger-Group-Simple | NON_CLUSTERED | 1473317340303 | 1473317345000 | 5 | ACQUIRED | NULL | NULL | 0 | 0 |
+-----------------+----------------------------+---------------+----------------------+---------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
1 row in set
可以看到Quart将任务信息放在了job,trriger中,触发的任务放在fire_trriger中
屏蔽一下代码,重新启动程序
/*JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("SimpleJob", "Job-Group-Simple").build(); // 触发时间点 // 交由Scheduler安排触发 String cronExpression = "0/5 * * * * ?"; // 每分钟的30s起,每5s触发任务 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("SimpleTrigger", "Trriger-Group-Simple") .withSchedule(cronScheduleBuilder) .build(); cronScheduler.scheduleJob(job, cronTrigger);*/
可以看到一下控制台输出
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: MLog clients using slf4j logging.
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Using default implementation for ThreadExecutor
九月 08, 2016 2:33:27 下午 org.quartz.simpl.SimpleThreadPool initialize
信息: Job execution threads will use class loader of thread: main
九月 08, 2016 2:33:27 下午 org.quartz.core.SchedulerSignalerImpl <init>
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
九月 08, 2016 2:33:27 下午 org.quartz.core.QuartzScheduler <init>
信息: Quartz Scheduler v.2.2.1 created.
九月 08, 2016 2:33:27 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport initialize
信息: Using thread monitor-based data access locking (synchronization).
九月 08, 2016 2:33:27 下午 org.quartz.impl.jdbcjobstore.JobStoreTX initialize
信息: JobStoreTX initialized.
九月 08, 2016 2:33:27 下午 org.quartz.core.QuartzScheduler initialize
信息: Scheduler meta-data: Quartz Scheduler (v2.2.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 5 threads.
Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler 'QuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
九月 08, 2016 2:33:27 下午 org.quartz.impl.StdSchedulerFactory instantiate
信息: Quartz scheduler version: 2.2.1
九月 08, 2016 2:33:27 下午 com.mchange.v2.log.slf4j.Slf4jMLog$Slf4jMLogger$InfoLogger log
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2sigdn9j2cwy271yl9aea|70e98812, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 2sigdn9j2cwy271yl9aea|70e98812, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 120, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Freed 1 triggers from 'acquired' / 'blocked' state.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovering 0 jobs that were in-progress at the time of the last shut-down.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Recovery complete.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 0 'complete' triggers.
九月 08, 2016 2:33:28 下午 org.quartz.impl.jdbcjobstore.JobStoreSupport recoverJobs
信息: Removed 1 stale fired job entries.
九月 08, 2016 2:33:28 下午 org.quartz.core.QuartzScheduler start
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED started.
//第一次
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 1
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
//第二次
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 2
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
//第三次
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:28 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:28,计数: 3
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:28
九月 08, 2016 2:33:28 下午 job.SimpleJob execute
警告: 已经暂停了触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:33:28 下午 job.SchedEventLister run
警告: 暂停当前线程5秒钟后在恢复触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
警告: 正在恢复了触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
//第四次
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
信息: ==================:job.SimpleJob was just triggered...
九月 08, 2016 2:33:33 下午 job.SchedEventLister <init>
信息: 触发器线程监听启动....
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
信息: 任务分组名称: Job-Group-Simple,任务名称: SimpleJob,触发器组名称: Trriger-Group-Simple,触发器名称: SimpleTrigger 执行时间: 2016-09-08 02:33:33,计数: 4
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。2016-09-08 02:33:33
九月 08, 2016 2:33:33 下午 job.SchedEventLister run
信息: 触发器线程监听中。。。。,状态:正常
九月 08, 2016 2:33:33 下午 job.SimpleJob execute
严重: 正在停止并且移除触发器...,触发器组名: Trriger-Group-Simple,触发器名称: SimpleTrigger
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler shutdown
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED shutting down.
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler standby
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED paused.
九月 08, 2016 2:35:28 下午 org.quartz.core.QuartzScheduler shutdown
信息: Scheduler QuartzScheduler_$_NON_CLUSTERED shutdown complete.
quartz.properties文件:
#线程池实现类
org.quartz.threadPool.class =org.quartz.simpl.SimpleThreadPool
#线程池数
org.quartz.threadPool.threadCount = 5
#线程池优先级
org.quartz.threadPool.threadPriority = 5
#线程池运行模式守后,还是前台
org.quartz.threadPool.makeThreadsDaemons=false
###线程池配置的继承属性
org.quartz.threadPool.threadsInheritGroupOfInitializingThread=true
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
###保存job调度执行信息到RAM,这种方式当应用下次重启时,job调度信息容易丢失
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#org.quartz.jobStore.misfireThreshold = 60000
############.JobStoreTX,是将job调度信息保存到RDB中,应用重启时可以重新调度job
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=test
#org.quartz.dataSource.test.connectionProvider.class=org.quartz.utils.PoolingConnectionProvider
org.quartz.dataSource.test.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.test.URL=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=utf8&characterSetResults\=utf8
org.quartz.dataSource.test.user=root
org.quartz.dataSource.test.password=123456
org.quartz.dataSource.test.maxConnections=20
总结:
从以上分析可以看出,JobStoreTX-RDB方式,实际上,就是将job存储在 qrtz_job_details表中,将cron_triggers存储在qrtz_cron_triggers,而触发的任务存储在qrtz_fired_triggers表中,应用崩溃时,而已触发的任务保存在qrtz_fired_triggers,下次应用启动时,直接调度qrtz_fired_triggers表中的任务。
备注:其他相关表
qrtz_paused_trigger_grps:暂定触发任务表
qrtz_triggers:触发任务表
qrtz_simple_triggers:简单触发器表
qrtz_scheduler_state:调度状态表
来看一下qrtz_triggers:
mysql> select * from qrtz_triggers;
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
| SCHED_NAME | TRIGGER_NAME | TRIGGER_GROUP | JOB_NAME | JOB_GROUP | DESCRIPTION | NEXT_FIRE_TIME | PREV_FIRE_TIME | PRIORITY | TRIGGER_STATE | TRIGGER_TYPE | START_TIME | END_TIME | CALENDAR_NAME | MISFIRE_INSTR | JOB_DATA |
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
| QuartzScheduler | SimpleTrigger | Trriger-Group-Simple | SimpleJob | Job-Group-Simple | NULL | 1473317345000 | 1473317340000 | 5 | ACQUIRED | CRON | 1473317339000 | 0 | NULL | 0 | |
+-----------------+---------------+----------------------+-----------+------------------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
1 row in set
发表评论
-
TreeSet在Quartz任务调度过程中的作用
2017-08-24 23:43 736红黑树详解:http://www.cnblogs.com/sk ... -
Quartz使用与Spring集成系列教程
2016-10-26 09:48 502Quartz的使用:http://donald-draper. ... -
Spring与Quartz集成-源码分析
2016-09-13 11:50 2723在阅读以下文章之前,如果对Quartz任务调度不是很熟悉,请看 ... -
Spring与Quartz集成详解
2016-09-09 17:52 2824首先这个所有的依赖包就不需要多讲了,首先下载Quazrt发布包 ... -
Quartz 任务存储JobStoreTX 持久化之RDB-源码分析
2016-09-08 18:24 2330Quartz 任务存储JobStoreTX 持久化之RDB:h ... -
Quartz任务调度源码分析
2016-09-07 13:12 3575Quartz的使用:http://donald-draper. ... -
Quartz的job调度源码分析
2016-09-06 13:03 8Quartz的使用:http://donald ... -
Quartzs的job,trriger监听器源码分析
2016-09-06 11:15 1488Quartz的使用:http://donald-draper. ... -
Quartz的job、触发器的暂停与恢复源码分析
2016-09-06 09:01 5274Quartz的使用:http://donald-draper. ... -
Quartz的Scheduler初始化源码分析
2016-09-05 16:14 3599Quartz的使用:http://donald ... -
Quartzs的job存储,触发器、job删除源码分析
2016-09-05 15:39 4575前言:unscheduleJob针对TriggerKey,而d ... -
Quartz的使用
2016-09-05 11:39 1793Quartz使用总结:http://www.cnblogs.c ...
相关推荐
任务调度的持久化功能,即新增修改删除之类的功能,这必须得要有的,不然都不知道后台都有什么作业在跑
在本文中,我们将深入探讨如何在Spring Boot 2.3版本中集成Quartz定时任务,并实现其持久化到数据库,以便支持集群环境。这个过程的关键在于配置Quartz Scheduler,设置数据库连接,以及确保任务在多节点环境中能够...
在这个基于Spring的配置中,我们将深入探讨如何在Spring应用中集成Quartz,实现任务的存储和持久化。 首先,我们需要在Spring配置文件中引入Quartz的相关bean。这通常包括`SchedulerFactoryBean`,它是Spring管理...
这个压缩包“Quartz.net 3.0.7.0数据库持久化.rar”包含了关于如何使用Quartz.NET实现任务调度的数据库持久化和集群配置的资料。Quartz.NET 3.0.7.0是该库的一个稳定版本,它提供了许多改进和新特性。 **数据库持久...
Quartz2.2.1版本在存储和持久化方面提供了多种机制,确保任务可以在系统重启后继续执行,增强了系统的可靠性和可扩展性。这篇博客文章详细介绍了Quartz在存储和持久化方面的实现方式和具体步骤。 首先,Quartz支持...
### Spring中的Quartz定时任务与持久化管理 #### 一、Spring与Quartz简介 Spring框架作为Java领域中广泛使用的轻量级应用框架之一,它提供了丰富的功能支持,包括依赖注入、面向切面编程以及MVC框架等。而Quartz是...
它的持久化功能是其重要特性之一,使得在系统重启后能够恢复先前的状态,继续执行预定的任务,保证了服务的连续性和可靠性。 Quartz 提供了多种持久化策略,包括 JDBC、JPA、Hibernate 和文本文件等。这些策略允许...
Quartz的持久化机制依赖于一组特定的数据库表,这些表存储了Job(任务)和Trigger(触发器)的信息。以下是Quartz默认使用的数据库表: 1. QRTZ_JOB_DETAILS:存储Job的详细信息,如Job类名、是否持久化、组名等。 ...
总结,Quartz 2.2.0的JobStore持久化到数据库功能提供了可靠的任务调度解决方案,通过配置数据源和JobStore类型,可以将作业和触发器的元数据存储在数据库中,实现跨应用和跨服务器的集群调度。开发者只需关注作业和...
在1.6.6版本中,Quartz提供了持久化功能,这意味着即使应用程序关闭或重启,已经定义的任务也能在下次启动时继续执行,无需重新配置。 在"quartz1.6.6 持久化实例"中,我们重点关注以下几个关键知识点: 1. **...
在“Quartz2.0持久化到数据库”这个主题中,我们将深入探讨如何将Quartz2.0的任务调度信息存储到数据库中,以便在系统重启或故障后能够恢复任务,并提供更可靠的调度服务。 首先,理解Quartz2.0的持久化机制至关...
【标题】:Quartz界面化持久化管理 【描述】:Quartz是一个开源的作业调度框架,用于在Java应用程序中创建和执行计划任务。通过结合文章《quartz界面化持久化管理》(链接:...
Quartz的定时任务持久化是指将任务的相关信息(如触发器、作业详情)存储到数据库中,而不是仅仅保留在内存里。这样做的好处是,即使应用服务器重启,之前设定的任务也不会丢失,因为它们可以从数据库中重新加载。...
在实际应用中,Quartz的持久化经常与Spring框架结合,通过Spring的数据源(dataSource)来实现任务的存储和恢复。 一、Quartz持久化的意义 1. 任务恢复:当系统发生故障或重启时,能够恢复之前设置的任务,保证任务...
在企业级应用中,为了保证服务的高可用性和任务的连续性,往往需要将调度信息持久化到数据库中,这就是所谓的Quartz持久化。Quartz2.2版本在前一版本的基础上进行了优化和增强,提供了更加稳定和灵活的持久化机制。 ...
实现数据库持久化,需要配置 Quartz 的 `JobStore` 实现,比如 `org.quartz.impl.jdbcjobstore.JobStoreTX` 或 `org.quartz.impl.jdbcjobstore.StdJDBCDelegate`,这些类会与数据库交互,存储和检索 Job 和 Trigger ...
在使用Quartz时,为了实现任务的持久化,即在系统重启后还能保留之前的任务设置,我们需要将任务的相关信息存储到数据库中。这涉及到一系列特定的数据库表,这些表构成了Quartz的持久化数据结构。 Quartz的持久化...
在1.8.6版本中,Quartz提供了数据库持久化的功能,这使得任务调度的状态能够在系统重启后得以恢复,增强了系统的可靠性和稳定性。数据库持久化是通过将作业和触发器的信息存储在关系型数据库中实现的。 Spring框架...
Quartz可以使用内存存储,但为了持久化任务,我们需要配置数据库存储。创建一个`quartz.properties`文件,设置数据库连接信息,并选择合适的JobStore: ```properties org.quartz.scheduler.instanceName = Default...
本文将深入探讨如何使用Quartz2.2.1在Java环境中配置MySQL作为任务的持久化存储,并提供一个简单的demo作为参考。 首先,我们需要了解Quartz的持久化机制。Quartz将任务和触发器的信息存储在数据库中,当应用程序...