Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年7月7日 09:06:09 星期四
http://fanshuyao.iteye.com/
一、问题描述
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执行一次,没有重复执行,这样就达到了定时器的集群效果,成功。
参考资料:
spring定时任务:http://fanshuyao.iteye.com/blog/2267243
Spring4整合quartz2.2定时任务:http://fanshuyao.iteye.com/blog/2309223
Tomcat+Nginx集群:http://fanshuyao.iteye.com/blog/2309601
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年7月7日 09:06:09 星期四
http://fanshuyao.iteye.com/
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群的更多相关文章
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...
- Spring 梳理 - 开启并配置 Spring MVC 的方法
传统web.xm中配置两个上下文+两个context对应的xml+两个上下文bean分别手动配置 传统web.xm中配置两个上下文+两个context对应的xml+<mvc:annotation ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- Spring+MyBatis实践—工程配置
初次实践:Spring+MyBatis技术搭建框架,采用Bootstrap前端开源框架. 简介: MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所 ...
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- Redis-5.0.0集群配置
版本:redis-5.0.0 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...
- Spring Boot 中如何配置 Profile
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- Redis-5.0.5集群配置
版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
随机推荐
- 通过live555实现H264 RTSP直播
http://blog.csdn.net/firehood_/article/details/16844397
- 单片机 C 语言模块化编程
好的开始是成功的一半 通过上一章的学习,我想你已经掌握了如何在程序中释放CPU了.希望能够继续坚持下去.一个良好的开始是成功的一半.我们今天所做的一切都是为了在单片机编程上做的更好. 在谈论今天的主题 ...
- 【Xamarin挖墙脚系列:Xamarin的核心】
原文:[Xamarin挖墙脚系列:Xamarin的核心] Xamarin 包含两个商业产品 :Xamarin.IOS, Xamarin.Android.他们都是通过开源的基于.Net的Mono项目构建 ...
- mysqll 数据库相互堵塞问题
192.168.11.186 远程访问192.168.11.185 数据库 186上看到: centos6.5:/root#mysql -uroot -p'kjk123123' -h192.168.1 ...
- 数据库设主键以及where的应用
二.第二课 create table teacher ( tno int primary key identity(1,1), --将tno设为主键(primary key identity(1,1 ...
- BZOJ3156: 防御准备
3156: 防御准备 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 442 Solved: 210[Submit][Status] Descript ...
- cocos2d-x学习笔记1——Cocos2D-x 中的核心类
Cocos2D-x 引擎的设计思路是将游戏的各个部分抽象成几个概念,包括导演.场景.布景层和人物精灵,它们之间的关系如图3-1 所示: 导演(CCDirector): 顾名思义,导演类是游戏中的组织者 ...
- [置顶]VC2013的一个bug
[置顶]VC2013的一个bug 前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其 ...
- nginx一致性hash及应用场景。
考虑一种场景. 多台web服务. 1 后台添加用户,更新用户信息,要求管理员能够实时看到变化. 2 前台用户允许1分钟后生效. nginx 配置一致性hash1. https://github.com ...
- cat、cp命令
cat是查看文件内容, cp –cp是连目录及件文件都拷贝 cp是拷贝文件 a.txt里的内容是, abc def ghi cat a.txt |grep –v ghi 得到结果, abc def h ...