Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

来源:互联网 发布:腾讯微信数据报告2017 编辑:程序博客网 时间:2024/05/18 03:24

一、问题描述

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代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.     <!-- 配置任务bean类 -->  
  7.     <bean id="quartzTask" class="com.lqy.spring.task.QuartzTask"></bean>  
  8.       
  9.     <!-- 配置方法映射工厂类 -->  
  10.     <!-- MethodInvokingJobDetailFactoryBean不支持序列化 -->  
  11.     <!-- <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  12.         <property name="targetObject" ref="quartzTask"></property>  
  13.         <property name="targetMethod" value="startTask"></property>  
  14.         <property name="concurrent" value="false"></property>  
  15.         concurrent : false表示等上一个任务执行完后再开启新的任务  
  16.     </bean> -->  
  17.     <!-- 配置方法映射工厂类 -->  
  18.     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
  19.         <property name="jobClass" value="com.lqy.spring.task.QuartzTaskExtends"></property>  
  20.         <property name="durability" value="true"></property>  
  21.         <property name="requestsRecovery" value="true" />   
  22.     </bean>  
  23.       
  24.     <!-- 配置任务高度的的时间/周期 -->  
  25.     <bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
  26.         <property name="jobDetail" ref="jobDetail"></property>  
  27.         <property name="cronExpression" value="0 */1 * * * ?"></property>  
  28.         <!-- <property name="startDelay" value="3000"></property> -->  
  29.     </bean>  
  30.       
  31.     <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  32.         <property name="dataSource" ref="dataSource"></property>    
  33.         <!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->      
  34.         <property name="overwriteExistingJobs" value="true" />      
  35.          <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->    
  36.         <property name="startupDelay" value="30" />    
  37.         <!-- 设置自动启动 -->    
  38.         <property name="autoStartup" value="true" />  
  39.         <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />    
  40.         <property name="configLocation" value="classpath:spring-quartz.properties" />    
  41.         <property name="triggers">  
  42.             <list>  
  43.                 <ref bean="jobTrigger"/>  
  44.             </list>  
  45.         </property>  
  46.     </bean>  
  47.       
  48. </beans>  

 

spring-quartz.xml文件里面主要配置的是定时任务

 

其中和一般Spring整合quartz定时任务不同的是:

(1)使用数据源:

在Bean id="schedulerFactoryBean"中使用了数据源:

Xml代码  收藏代码
  1. <property name="dataSource" ref="dataSource"></property>    

 数据源dataSource是在Spring.xml文件配置的。

 

(2)jobDetail使用的是org.springframework.scheduling.quartz.JobDetailFactoryBean

由于集群需要把定时任务的信息写入表,需要序列化吧,但MethodInvokingJobDetailFactoryBean 不能序列化,会报错。

 

(3)增加spring-quartz.properties文件

Xml代码  收藏代码
  1. #==============================================================    
  2. #Configure Main Scheduler Properties    
  3. #==============================================================     
  4. org.quartz.scheduler.instanceName = defaultScheduler  
  5. org.quartz.scheduler.instanceId = AUTO  
  6.   
  7. #==============================================================    
  8. #Configure JobStore    
  9. #==============================================================   
  10. orgorg.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
  11. orgorg.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
  12. org.quartz.jobStore.tablePrefix = QRTZ_  
  13. org.quartz.jobStore.isClustered = true  
  14. org.quartz.jobStore.clusterCheckinInterval = 20000    
  15. org.quartz.jobStore.dataSource = myDS  
  16. org.quartz.jobStore.maxMisfiresToHandleAtATime = 1  
  17. org.quartz.jobStore.misfireThreshold = 120000  
  18. org.quartz.jobStore.txIsolationLevelSerializable = true  
  19.    
  20. #==============================================================    
  21. #Configure ThreadPool    
  22. #==============================================================   
  23. orgorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
  24. org.quartz.threadPool.threadCount = 10  
  25. org.quartz.threadPool.threadPriority = 5  
  26. org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true  
  27.   
  28. #==============================================================  
  29. #Skip Check Update  
  30. #update:true  
  31. #not update:false  
  32. #==============================================================  
  33. org.quartz.scheduler.skipUpdateCheck = true   
  34.   
  35. #============================================================================     
  36. # Configure Plugins      
  37. #============================================================================        
  38. orgorg.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin     
  39. orgorg.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin  
  40. org.quartz.plugin.shutdownhook.cleanShutdown = true  

 

这个配置文件也是Quartz的配置文件,本人也不了解,只是从网上搜索来的。本来是有数据库的连接配置的,但由于spring配置文件已经有数据源,我就删除掉了。

 

4、把上面的spring-quartz.xml 引入到spring.xml文件中:

Xml代码  收藏代码
  1. <import resource="spring-quartz.xml"/>  

 或者在web.xml文件加上,这种方法没有尝试,但应该是可以的:

Xml代码  收藏代码
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>classpath:spring.xml,classpath:spring-quartz.xml</param-value>  
  4. </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/

  • quartz-2.2.3-distribution.tar.gz (3.2 MB)
  • 下载次数: 69
0 0
原创粉丝点击