quartz的集群

quartz开始就是支持集群的,开始的时候懒得看文档,自己写了个方案到数据库做管理,但对开发人员要求较高,常常出问题 
重新看了下,不是很复杂。 
使用的quartz版本是1.8.4,spring版本是3.0.3 
开始使用的是quartz2.1.7,后来集成spring的时候,总是不成功,查了资料才知道spring需要3.2才能集成quartz2,在升级spring和降低quartz的选择下,选择了降低quartz。 
下面开始: 
1.编写需要定时执行的代码QuartzTest.java 

package cn.zan.util;  
  
import java.io.Serializable;  
import java.util.Date;  
  
public class QuartzTest implements Serializable{  
    /** 
     * 此处必须实现序列号,不然无法集群 
     */  
    private static final long serialVersionUID = -2073310586499744415L;  
  
    public void execute(){  
        //System.out.println("quartz test execute,every 10 seconds");  
        Date date=new Date();  
        System.out.println(date.toLocaleString());  
          
    }  
} 

  

2.将此bean用spring管理 

<bean name="quartzTestBean" class="cn.zan.util.QuartzTest" scope="prototype">  
</bean> 

3.定义jobdetail 

<bean name="quartzTestJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
            <property name="targetObject" ref="quartzTestBean" />    
           <property name="targetMethod" value="execute"/>    
</bean> 

  

4.定义一个trigger,每十秒一趟 

<bean name="quartzTestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">    
        <property name="jobDetail" ref="quartzTestJobDetail" />  
        <property name="cronExpression" value="0/10 * * * * ?" />  
</bean>  

5.初始化scheduler,并指定了quartz配置文件的位置 

<!--此bean的name可以随意,spring手册上这块甚至是空着的 -->  
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
       <property name="configLocation" value="classpath:quartz.properties"/>  
            <property name="triggers">    
                <list>    
                      <ref bean="quartzTestJobTrigger"/>  
               </list>    
          </property>    
     </bean>   

6.建立quartz集群的数据库,用的是mysql,脚本在附件 
7.配置quartz,指定数据库名称、用户名、密码等 

#============================================================================  
# Configure Main Scheduler Properties    
#============================================================================  
#唯一标识,同一个集群的名称必须一致  
org.quartz.scheduler.instanceName = MyClusteredScheduler  
#auto,则quartz会根据时间和主机名生成,确保唯一  
org.quartz.scheduler.instanceId = AUTO  
  
  
#============================================================================  
# Configure ThreadPool    
#============================================================================  
  
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
org.quartz.threadPool.threadCount = 25  
org.quartz.threadPool.threadPriority = 5  
  
#============================================================================  
# Configure JobStore    
#============================================================================  
  
org.quartz.jobStore.misfireThreshold = 60000  
  
#jobstoretx则任务会被持久化到数据中,默认为RAMJobStore,默认会被维护到内存中,集群的时候必须修改  
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
org.quartz.jobStore.useProperties = false  
org.quartz.jobStore.dataSource = myDS  
org.quartz.jobStore.tablePrefix = QRTZ_  
  
#ture则此实例需要参加到集群中  
org.quartz.jobStore.isClustered = true  
org.quartz.jobStore.clusterCheckinInterval = 20000  
  
#============================================================================  
# Configure Datasources    
#============================================================================  
  
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver  
org.quartz.dataSource.myDS.URL = jdbc\:mysql\://192.168.1.109\:3306/test?useUnicode\=true&characterEncoding\=utf-8  
org.quartz.dataSource.myDS.user = root  
org.quartz.dataSource.myDS.password = 123456  
org.quartz.dataSource.myDS.maxConnections = 10  
org.quartz.dataSource.myDS.validationQuery=select 0 from dual  

  

8.理论上这就行了,实际上你会发现这不行,spring的bug啊,附件有两个类,BeanInvokingJobDetailFactoryBean、MethodInvokingJobDetailFactoryBean,重写下默认包里的,这两个内容来自https://jira.springsource.org/browse/SPR-3797 
这样,你再启动的时候,就能看到如下内容,证明集群成功了 

3-5-23 21:49:04 org.springframework.scheduling.quartz.SchedulerFactoryBean initSchedulerFactory  
信息: Loading Quartz config from [class path resource [quartz.properties]]  
2013-5-23 21:49:04 org.quartz.core.SchedulerSignalerImpl <init>  
信息: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl  
2013-5-23 21:49:04 org.quartz.core.QuartzScheduler <init>  
信息: Quartz Scheduler v.1.8.4 created.  
2013-5-23 21:49:04 org.quartz.impl.jdbcjobstore.JobStoreSupport initialize  
信息: Using db table-based data access locking (synchronization).  
2013-5-23 21:49:04 org.quartz.impl.jdbcjobstore.JobStoreTX initialize  
信息: JobStoreTX initialized.  
2013-5-23 21:49:04 org.quartz.core.QuartzScheduler initialize  
信息: Scheduler meta-data: Quartz Scheduler (v1.8.4) 'quartzScheduler' with instanceId 'frady0021369316944640'  
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.  
  NOT STARTED.  
  Currently in standby mode.  
  Number of jobs executed: 0  
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 25 threads.  
  Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is clustered.  
  
2013-5-23 21:49:04 org.quartz.impl.StdSchedulerFactory instantiate  
信息: Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.  
2013-5-23 21:49:04 org.quartz.impl.StdSchedulerFactory instantiate  
信息: Quartz scheduler version: 1.8.4  
2013-5-23 21:49:04 org.quartz.core.QuartzScheduler setJobFactory  
信息: JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@dd20b6  

  

9.that's all,enjoy it. 
一个要注意的问题:如果集群的容易在不同的物理服务器上,那么必须有一个时间同步的方案,来确保时间的同步。可以使用NTP,具体参照:http://stunizhengjia.iteye.com/admin/blogs/2228199

附件包含上面提到和写到的几处内容。 
不包含如何搭建spring环境,这个我假设你已经会了 
最后试了下目前最新版本的quartz和spring,在集群的时候仍然会有class序列化的问题,看来spring根本就不鸟quartz,这样一个简单的问题,一直都不给于解决。 

猜你喜欢

转载自stunizhengjia.iteye.com/blog/2228197