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定时任务集群相关推荐
- quartz mysql 表 集群配置_Spring整合实战丨Quartz 集群配置
Quartz说明 1.quartz可以通过jdbc直连连接到MYSQL数据库,读取配置在数据库里的job初始化信息,并且把job通过java序列化到数据库里,这样就使得每个job信息得到了持久化,即使 ...
- 学习Spring(一) -- 配置Spring
2019独角兽企业重金招聘Python工程师标准>>> 下载Spring 可以直接访问spring的资源库,http://repo.springsource.org/libs-rel ...
- Spring Cloud Config 集中式配置
2019独角兽企业重金招聘Python工程师标准>>> 本指南通过Spring cloud config服务器引导你建立和消费配置. 你要构建什么? 你需要设置一个config服务器 ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- Spring 3.1 事务配置
版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring 3.1 事务配置 spring发的包最好用的是2.5.6:所依赖的包全部都有,可是后来的版本都缺这少那的,导致开发中遇到各种各样的问题 ...
- Redis-5.0.5集群配置
Redis-5.0.5集群配置 版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://gith ...
- Spring Boot 排除自动配置
Spring Boot 排除自动配置 Spring Boot 自动配置非常强大,有时需要排除/禁用 Spring Boot 某些类的自动化配置. *************************** ...
- springboot跳转html_畅游Spring Boot系列 — 自定义配置
这里要说的自定义配置主要是两类:一类是关于Spring MVC的扩展配置,一类是Spring Boot自身通过配置文件的自定义配置 首先,我们来看一下SpringBoot中关于Spring MVC的相 ...
- Spring中定时任务Quartz集群配置学习
Spring中定时任务Quartz集群配置学习 原来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运行,没 ...
- quartz集群分布式(并发)部署解决方案-Spring
项目中使用分布式并发部署定时任务,多台跨JVM,按照常理逻辑每个JVM的定时任务会各自运行,这样就会存在问题,多台分布式JVM机器的应用服务同时干活,一个是加重服务负担,另外一个是存在严重的逻辑问题, ...
最新文章
- 系统上线后关键用户的工作建议
- 【剪枝算法】通过网络瘦身学习高效的卷积网络Learning Efficient Convolutional Networks through Network Slimming论文翻译
- 【译】Effective TensorFlow Chapter10——在TensorFlow中利用多GPU处理并行数据
- 给Android程序员的一些面试建议,附带学习经验
- 去除list集合中重复项的几种方法
- mac pandas文件路径_Mac进阶必看:如何利用Automator快速获取文件路径
- mysql win 64_win10下装mysql-5.7.18-winx64
- 聊聊lettuce的sentinel连接
- [Silverlight入门系列]用TransformToVisual和Transform取得元素绝对位置(Location)
- Redis入门到入土教程_1
- 「镁客·请讲」问之科技翟鲁峰:希望可以用声音传递简易生活方式
- IFI Claims:2018年中国企业在美国申请专利数量7298件
- DDR SDRAM内存发展历程
- 【VBS脚本】VBS复制Excel工作簿
- C++笔记 文件处理笔记
- 【T3】打印单据(非新打印)表头显示不全
- qq游戏英雄杀怎么老是显示计算机,qq英雄杀老版本
- Android 源码刷机版本号、支持设备对照表(AOSP )
- LintCode 买卖股票的最佳时机I II III IV
- Greenplum非并行数据迁移--pg_dump