---
title: "将单机时钟任务升级到集群"
author: "wokeke"
date: "2015-04-13T14:06:55Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2015-04-13T14:07:29Z"
id: 2201898
url: "https://wokeke.iteye.com/blog/2201898"
---

单位项目中对于备份日志、邮件发送等日常工作，使用Spring下的quartz以定时任务的方式处理。但随着将整个系统升级到集群环境后，问题来了。多个节点到底由哪个节点来执行这些任务呢？

 

      起初的做法是，由集群中权重较小的一个节点担当。具体做法是在发布程序时只在此节点applicationContext-schedule.xml中的调度器(Scheduler)配置中添加相应的触发器(Trigger)。

 

      以上方法虽然笨，但解决了燃眉之急。手头没有那么多着急的需求时还是想用更靠谱的方式来替代它。于是准备采用Spring+Quartz集群。

 

      重点参数了几下几篇博文：

      1.[完美解决多应用服务器负载均衡环境下spring quartz同一定时任务重复执行问题](http://medbricom.iteye.com/blog/1782765) 

      2.[Spring+Quartz 集群](http://soulshard.iteye.com/blog/337886)  

 

Quartz的官网地址如下，数据表创建脚本及官方实例都在包里呢：

[http://www.quartz-scheduler.org/](http://www.quartz-scheduler.org/)

 

以下是对于Quartz.properties的相关属性一些说明(*在官方实例example13配置文件基础上修改*)

```
#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
##可为任何值，用在 JDBC JobStore 中来唯一标识实例，但是所有集群节点中必须相同。
org.quartz.scheduler.instanceName = TestScheduler
##属性为 AUTO即可，基于主机名和时间戳来产生实例 ID,不为AUTO需要保持各个节点间不一致
org.quartz.scheduler.instanceId = instance_one

org.quartz.scheduler.skipUpdateCheck = true

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

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

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

org.quartz.jobStore.misfireThreshold = 60000

##属性为 JobStoreTX，将任务持久化到数据中。因为集群中节点依赖于数据库来传播 Scheduler 实例的状态，你只能在使用 JDBC JobStore 时应用 Quartz 集群。这意味着你必须使用 JobStoreTX 或是 JobStoreCMT 作为 Job 存储；你不能在集群中使用 RAMJobStore。
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.dataSource=myDS
org.quartz.jobStore.tablePrefix=QRTZ_
##属性为 true，你就告诉了 Scheduler 实例要它参与到一个集群当中。这一属性会贯穿于调度框架的始终，用于修改集群环境中操作的默认行为。
org.quartz.jobStore.isClustered=true
#org.quartz.jobStore.clusterCheckinInterval 属性定义了Scheduler 实例检入到数据库中的频率(单位：毫秒)。Scheduler 检查是否其他的实例到了它们应当检入的时候未检入；这能指出一个失败的 Scheduler 实例，且当前 Scheduler 会以此来接管任何执行失败并可恢复的 Job。通过检入操作，Scheduler 也会更新自身的状态记录。clusterChedkinInterval 越小，Scheduler 节点检查失败的 Scheduler 实例就越频繁。默认值是 15000 (即15 秒)。

#============================================================================
# Other Example Delegates
#============================================================================
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.CloudscapeDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v6Delegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v7Delegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DriverDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PointbaseDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.WebLogicDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.WebLogicOracleDelegate

#============================================================================
# Configure Datasources (PostgreSQL)
#============================================================================

org.quartz.dataSource.myDS.driver = org.postgresql.Driver
org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost:5432/quartz
org.quartz.dataSource.myDS.user = quartz
org.quartz.dataSource.myDS.password = quartz
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=

#==============================================================  
#Configure DataSource  (MySQL)
#============================================================== 
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz?useUnicode=true&amp;characterEncoding=UTF-8
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = 123456
org.quartz.dataSource.myDS.maxConnections = 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
```