Quartz(05) job 的持久化

x33g5p2x  于2021-12-21 转载在 其他  
字(1.9k)|赞(0)|评价(0)|浏览(367)

在上一章(Quartz(04) Quartz 基本配置)中,我们在quartz.properties 文件中配置了org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore , 这个配置的意思,将job运行的信息存放在内存中,Quartz也提供了另外一种持久化job运行信息的方式,存储到数据库! 比如一个任务每分钟执行一次,但是由于机器故障,服务器停止运行了一个小时,如果把job的信息存放到内存中,显然会造成信息的丢失. Quartz 便提供了一种把这些信息存储到数据的功能.

源码下载地址

Quartz 提供了两种持久化的方式,JobStoreTX和JobStoreCMT.本文以JobStoreTX讲解.在做这个功能之前,我们需要建立一套Quartz需要使用的表.这个在Quartz的文档中,针对不同数据库有不同的sql文件,选择合适的sql,在数据库中建立相应的表.

然后我们去修改 quartz.properties文件就好了.当然jdbc的jar包少不了.quartz.properties的文件如下.

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

#jobStoreTX TEST
org.quartz.jobStore.misfireThreshold=60000
# TX method
# jobStore 的方式为obStoreTX
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc Delegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#table prefix
org.quartz.jobStore.tablePrefix=qrtz_
#数据源的名称
org.quartz.jobStore.dataSource=myDS
#数据库连接信息,注意myDS 是和数据源同名的,上一行
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver   
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user=root   
org.quartz.dataSource.myDS.password=123456   
org.quartz.dataSource.myDS.maxConnections=10

#Configure Plugins 
#插件
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames=quartz_jobx.xml

这样就大功告成了.在以后我们讲到Quartz与Web/Spring的整合,并且将所有的job信息的配置到数据的表中,quartz_jobx.xml 文件就不在需要了.
下一章节Quartz(06) quartz整合到web项目中

相关文章