Spring+quartz集群解决多服务器部署定时器重复执行的问题
一、问题描述
Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了。不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行。
二、解决方案
Spring+quartz集群可以解决多服务器部署定时器重复执行的问题。
1、下载quartz的Jar包或者在Maven项目加入quartz的依赖包
不再细说,详情可参考:
Spring4整合quartz2.2定时任务:http://fanshuyao.iteye.com/blog/2309223
2、建立quartz表
quartz是通过表来实现多服务器定时任务集群的,表的详细信息在压缩包:quartz-2.2.3-distribution.tar.gz,
路径为:quartz-2.2.3-distribution\quartz-2.2.3\docs\dbTables
这里有很多表的SQL可执行语句,直接复制执行即可。
其中附件有quartz-2.2.3-distribution.tar.gz,是2016-07-07最新的。
本人使用的是Mysql,执行的是tables_mysql_innodb.sql,另外一个tables_mysql.sql没执行,看都没有看,哈哈。
注:最好先建完表,再执行后面的索引,不然会有警告。
里面的表详细本人也不了解,就不说了,百度或Google吧。
3、新增一个Bean文件(spring-quartz.xml),如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置任务bean类 -->
<bean id="quartzTask" class="com.lqy.spring.task.QuartzTask"></bean> <!-- 配置方法映射工厂类 -->
<!-- MethodInvokingJobDetailFactoryBean不支持序列化 -->
<!-- <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartzTask"></property>
<property name="targetMethod" value="startTask"></property>
<property name="concurrent" value="false"></property>
concurrent : false表示等上一个任务执行完后再开启新的任务
</bean> -->
<!-- 配置方法映射工厂类 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.lqy.spring.task.QuartzTaskExtends"></property>
<property name="durability" value="true"></property>
<property name="requestsRecovery" value="true" />
</bean> <!-- 配置任务高度的的时间/周期 -->
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="0 */1 * * * ?"></property>
<!-- <property name="startDelay" value="3000"></property> -->
</bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
<property name="overwriteExistingJobs" value="true" />
<!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
<property name="startupDelay" value="30" />
<!-- 设置自动启动 -->
<property name="autoStartup" value="true" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:spring-quartz.properties" />
<property name="triggers">
<list>
<ref bean="jobTrigger"/>
</list>
</property>
</bean> </beans>
spring-quartz.xml文件里面主要配置的是定时任务
其中和一般Spring整合quartz定时任务不同的是:
(1)使用数据源:
在Bean id="schedulerFactoryBean"中使用了数据源:
<property name="dataSource" ref="dataSource"></property>
数据源dataSource是在Spring.xml文件配置的。
(2)jobDetail使用的是org.springframework.scheduling.quartz.JobDetailFactoryBean
由于集群需要把定时任务的信息写入表,需要序列化吧,但MethodInvokingJobDetailFactoryBean 不能序列化,会报错。
(3)增加spring-quartz.properties文件
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = defaultScheduler
org.quartz.scheduler.instanceId = AUTO #==============================================================
#Configure JobStore
#==============================================================
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.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.maxMisfiresToHandleAtATime = 1
org.quartz.jobStore.misfireThreshold = 120000
org.quartz.jobStore.txIsolationLevelSerializable = true #==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true #==============================================================
#Skip Check Update
#update:true
#not update:false
#==============================================================
org.quartz.scheduler.skipUpdateCheck = true #============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
这个配置文件也是Quartz的配置文件,本人也不了解,只是从网上搜索来的。本来是有数据库的连接配置的,但由于spring配置文件已经有数据源,我就删除掉了。
4、把上面的spring-quartz.xml 引入到spring.xml文件中:
<import resource="spring-quartz.xml"/>
或者在web.xml文件加上,这种方法没有尝试,但应该是可以的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-quartz.xml</param-value>
</context-param>
5、把项目部署到集群环境中
其实就是把项目部署到2个Tomcat中,这样能看到定时任务是否重复执行。
Tomcat+Nginx集群详情见:http://fanshuyao.iteye.com/blog/2309601
6、启动所有Tomcat,看结果,如下:

由图可见:定时器是每分钟的0秒执行一次
在2个Tomcat中,分别是一个tomcat执行一次,另一个tomcat执行一次,没有重复执行,这样就达到了定时器的集群效果,成功。
想到的其他方案
1.全局锁机制
对于只需要执行一次初始化的操作,在全局缓存中设置一个全局锁,拿到这个锁的节点执行相应的任务。
2.数据库层面的状态标记
对于有些定时任务,我们只希望执行一次,也就是说多个节点,只有一个节点在执行这个定时任务
可以在数据库对这个任务进行状态标记,根据状态来调节节点间定时任务的执行
3.代码分离
如果有可能,将定时任务独立出来,成为一个单独的项目工程,部署单节点,避免集群部署
4.设置执行定时任务的机器名,在代码中判断
转自https://blog.csdn.net/edward1987815/article/details/80417564
Spring+quartz集群解决多服务器部署定时器重复执行的问题的更多相关文章
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Spring+Quartz集群环境下定时调度的解决方案
集群环境可能出现的问题 在上一篇博客我们介绍了如何在自己的项目中从无到有的添加了Quartz定时调度引擎,其实就是一个Quartz 和Spring的整合过程,很容易实现,但是我们现在企业中项目通常都是 ...
- Spring+Quartz 集群
这几天给Spring+Quartz的集群折腾得死去活来,google了无数页总算搞定,记下一些要点备以后使用. 单独的Quartz集群在http://unmi.blogjava.net/有Unmi翻译 ...
- 序列化人人网框架下的DAO?也就是在Spring下序列化DAO的问题(spring+quartz集群下)
人人网框架地址:http://code.google.com/p/paoding-rose/ 问题发生: 用Quartz作集群时用JobDataMap传递DAO,提示DAO未序列化,可框架的DAO为接 ...
- Tomcat学习总结(8)——Tomcat+Nginx集群解决均衡负载及生产环境热部署
近日,为解决生产环境热部署问题,决定在服务器中增加一个tomcat组成集群,利用集群解决热部署问题. 这样既能解决高并发瓶颈问题,又能解决热部署(不影响用户使用的情况下平滑更新生产服务器)问题. 因为 ...
- (4) Spring中定时任务Quartz集群配置学习
原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...
- 使用fabric解决百度BMR的spark集群各节点的部署问题
前言 和小伙伴的一起参加的人工智能比赛进入了决赛之后的一段时间里面,一直在构思将数据预处理过程和深度学习这个阶段合并起来.然而在合并这两部分代码的时候,遇到了一些问题,为此还特意写了脚本文件进行处理. ...
- Spring集成quartz集群配置总结
1.spring-quartz.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be ...
- Quartz Spring分布式集群搭建Demo
注:关于单节点的Quartz使用在这里不做详细介绍,直接进阶为分布式集群版的 1.准备工作: 使用环境Spring4.3.5,Quartz2.2.3,持久化框架JDBCTemplate pom文件如下 ...
随机推荐
- ajax跨域请求使用代理
public class ProxyHandler extends HttpServlet { private static final long serialVersionUID = 1L; ...
- php array_unshift,array_push追加数组元素
追加元素在数组前面:<?php $a=array("a"=>"Cat","b"=>"Dog"); ar ...
- MyBatis - Generator(MBG)
前言 MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器, 可以快速的根据数据表自动生成Bean对象.Java接口及SqlMapper.xml配置文件. ...
- JS中鲜为人知的问题: [] == ![]结果为true,而 {} == !{}却为false
console.log( [] == ![] ) // true console.log( {} == !{} ) // false 在比较字符串.数值和布尔值的相等性时,问题还比较简单.但在涉及到对 ...
- 【JZOJ3318】Brunhilda的生日
description 除去对铁质盔甲强烈的热爱,Brunhilda是一个正常的7岁女孩.近期,她正在策划一个完美的生日派对.她发明了如下的一个游戏:所有的孩子在一个数k被宣读之前不停地跑来跑去.当这 ...
- Java实现RSA加密
末尾贴上代码↓↓↓↓↓↓↓↓↓↓↓↓ 1.原理 2.实现过程 3. 公式 4.举例 p=13, q=11 , (p,q互质) N=p*q=143 L=(p-1)*(q-1)=120 E=7 ...
- csp-s模拟测试56Merchant, Equation,Rectangle题解
题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...
- 《你不知道的javascript》上卷笔记整理(一)
函数声明和变量声明都会被提升,但函数声明会被提升到普通变量前,而 var foo = function bar(){}; 赋值操作不会被提升. 闭包: 基于词法作用域(作用域是根据名称查找变量的一套规 ...
- 反编译之jd-gui的安装
1.下载JD-GUI http://jd.benow.ca/ 2.下载的dmg安装一直失败 通过brew(https://brew.sh/index_zh-cn.html)命令安装 brew cas ...
- 转载:python操作excel表格(xlrd/xlwt)
python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...