Quartz使用- Quartz的Job存储及集群部署

1. Job的存储与持久化

  Quartz的JobStore接口定义了作业Job、触发器trigger、调度器Scheduler等数据存储机制。Quartz主要有两种Job存储类型:内存存储RAMJobStore和持久化存储JDBCJobStore。下面将对其一一介绍。

2. RAMJobStore

  RAMJobStore是将Quartz涉及到的Job、Trigger、Scheduler等信息存储在内存中,是Quartz默认使用且最有效的Job存储方式。但其缺点也显而易见,一旦Quartz程序停止,内存中的Job信息将会丢失。

  使用RamJobStore时,如果未设置Quartz的配置文件quartz.properties,则编写Quartz程序默认使用RAMJobStore,如果需要在quartz.properties中进行设置"org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore",可参考如下:

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

org.quartz.scheduler.instanceName: TestScheduler
org.quartz.scheduler.instanceId: AUTO

org.quartz.scheduler.skipUpdateCheck: true

#============================================================================
# Configure ThreadPool  
#============================================================================

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 3
org.quartz.threadPool.threadPriority: 5

#============================================================================
# Configure JobStore  
#============================================================================

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

#============================================================================
# Configure Plugins 
#============================================================================

org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin

org.quartz.plugin.jobInitializer.class: org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames: quartz_data.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound: true
org.quartz.plugin.jobInitializer.scanInterval: 120
org.quartz.plugin.jobInitializer.wrapInUserTransaction: false

 

3. JDBCJobStore

  JDBCJobStore是将Quartz涉及的Job、Trigger、Scheduler通过JDBC存储到数据库中。与RAMJobStore相比,JDBCJobStore的配置稍微复杂些,且效率相对低。但其优点在于可以将Quartz的运行信息持久化。JDBCJobStore基于对所有的数据库有效,包括:Oracle, PostgreSQL, MySQL, MS SQL Server, HSQLDB, DB2等。当使用JDBCJobStore时,需要设置配置文件、以及创建相关的数据库表。

  1) 设置quartz.properties

  在quartz.preperties中设置JDBCJobStore,只需设置"org.quartz.jobStore.class = org.quartz.simpl.JDBCJobStore",以及设置对应的数据表即可。具体配置信息参考如下(下表为mysql的配置信息):

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

org.quartz.scheduler.instanceName: OcpScheduler
org.quartz.scheduler.instanceId: OcpInstance_1

org.quartz.scheduler.skipUpdateCheck: true

#============================================================================
# Configure ThreadPool  
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 50
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.misfireThreshold: 120000

org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties: false
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.dataSource: ocpQzDs
org.quartz.jobStore.isClustered: true
org.quartz.jobStore.clusterCheckinInterval = 60000 

#============================================================================
# Configure Datasources  
#============================================================================
org.quartz.dataSource.ocpQzDs.driver: com.mysql.jdbc.Driver
org.quartz.dataSource.ocpQzDs.URL:jdbc:mysql://127.0.0.1:3306/ocp?useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.ocpQzDs.user: test  
org.quartz.dataSource.ocpQzDs.password: test  
org.quartz.dataSource.ocpQzDs.maxConnection: 30

#============================================================================
# Configure Plugins 
#============================================================================
org.quartz.plugin.shutdownHook.class: org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownHook.cleanShutdown: true


#org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin

  2) 创建数据表

  将数据库及表的创建写入sql文件,导入即可。相关的表如下所示:  

表名

说明

QRTZ_FIRED_TRIGGERS

存存放已触发的触发器

QRTZ_PAUSED_TRIGGER_GRPS

存放暂停掉的触发器

QRTZ_SCHEDULER_STATE

调度器状态

QRTZ_LOCKS

存储程序的悲观锁的信息

QRTZ_SIMPLE_TRIGGERS

简单触发器的信息

QRTZ_SIMPROP_TRIGGERS

 

QRTZ_CRON_TRIGGERS

存放cron类型的触发器

QRTZ_BLOB_TRIGGERS

以Blob 类型存储的触发器

QRTZ_TRIGGERS

触发器的基本信息

QRTZ_JOB_DETAILS

存放一个jobDetail信息

QRTZ_CALENDARS

存放日历信息

4. Quarz应用集群

  Quartz集群架构如下,集群中的每个节点是一个独立的Quartz应用,且独立的Quartz节点并不与另一节点通信,而是通过相同的数据库表来感知另一Quartz应用。简而言之,Quartz应用、数据库支撑、多节点部署即可搭建起Quartz的应用集群

  Quartz集群还在同一个数据库下,由数据库中的数据来确定任务是否正在执行,如果该任务正在执行,则其他服务器就不能去执行该调度任务。Quartz集群的特点如下:

  1) 持久化

  Quartz可以将调度器scheduler、触发器trigger以及任务Job的运行时信息存储至数据库中,采用JDBCJobStore,如果服务器异常时,可以基于数据库中的存储信息进行任务恢复。

  2) 高可用性

  如果相关服务器节点挂掉的话,集群的其他节点则会继续执行相关任务。

  3) 伸缩性

  如果集群中的节点数过少,导致相关任务无法及时执行,可以增加额外的服务器节点,只需将其他节点上的脚本及配置信息拷贝至新部署的节点上运行即可。

  4) 负载均衡

  Quartz使用随机的负载均衡算法,任务job是以随机的方式由不同的节点上Scheduler实例来执行。但当前不存在一个方法指派一个Job到集群中的特定节点。

 

5. 下节概述

  下一节会将项目中应用Quartz模块相关方案设计、流程架构、及重点代码一一呈现。

猜你喜欢

转载自blog.csdn.net/qq_30336433/article/details/80924876
今日推荐