spring quartz集群配置

本示例基于spring quartz 1.6.5
1、创建数据库表
//在数据库中建表。建表模版在Quartz包下docs/dbTables下,选择相应的数据库和版本即可。
找到所需要的数据库对应的数据库表即可

2、 配置数据库连接池,如果spring已经配置则不需要再另行配置,只需在后面配置的applicationContext-quartz.xml引入即可。
applicationContext.xml:
	<context:component-scan base-package="com.sundoctor"/>
	<!-- 属性文件读入 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
	<!-- 数据源定义,使用c3p0 连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	<property name="driverClass" value="${jdbc.driverClassName}" />	
	<property name="jdbcUrl" value="${jdbc.url}" />	
	<property name="user" value="${jdbc.username}" />	
	<property name="password" value="${jdbc.password}" />		
		<property name="initialPoolSize" value="5" />
		<property name="minPoolSize" value="5" />
		<property name="maxPoolSize" value="20" />
		<property name="acquireIncrement" value="2" />
		<property name="maxIdleTime" value="3600" />
		<property name="idleConnectionTestPeriod"  value="180"/>  
		<property name="automaticTestTable" value="C3P0TESTTABLE"/> 
	</bean>		

jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lite_basic_db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
jdbc.username=root
jdbc.password=123

3、 配置quartz.properties
org.quartz.scheduler.instanceName属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。 
org.quartz.scheduler.instanceId 属性为 AUTO即可,基于主机名和时间戳来产生实例 ID。 
org.quartz.jobStore.class属性为 JobStoreTX,将任务持久化到数据中。因为集群中节点依赖于数据库来传播 Scheduler 实例的状态,你只能在使用 JDBC JobStore 时应用 Quartz 集群。这意味着你必须使用 JobStoreTX 或是 JobStoreCMT 作为 Job 存储;你不能在集群中使用 RAMJobStore。 
org.quartz.jobStore.isClustered 属性为 true,你就告诉了 Scheduler 实例要它参与到一个集群当中。这一属性会贯穿于调度框架的始终,用于修改集群环境中操作的默认行为。 
org.quartz.jobStore.clusterCheckinInterval 属性定义了Scheduler 实例检入到数据库中的频率(单位:毫秒)。Scheduler 检查是否其他的实例到了它们应当检入的时候未检入;这能指出一个失败的 Scheduler 实例,且当前 Scheduler 会以此来接管任何执行失败并可恢复的 Job。通过检入操作,Scheduler 也会更新自身的状态记录。clusterChedkinInterval 越小,Scheduler 节点检查失败的 Scheduler 实例就越频繁。默认值是 15000 (即15 秒)。

quartz.properties
#==============================================================  
#Configure Main Scheduler Properties  
#==============================================================   
org.quartz.scheduler.instanceName = TestScheduler1
org.quartz.scheduler.instanceId = AUTO  

#==============================================================  
#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

#==============================================================  
#Configure JobStore  
#============================================================== 
org.quartz.jobStore.misfireThreshold = 60000
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.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true  
org.quartz.jobStore.clusterCheckinInterval = 20000  


# Using datasource
org.quartz.jobStore.dataSource = qzDS


#Define the datasource to use


org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL = jdbc:mysql://localhost:3306/lite_basic_db
org.quartz.dataSource.qzDS.user = root
org.quartz.dataSource.qzDS.password = 123
org.quartz.dataSource.qzDS.maxConnections = 30
      

4、 配置applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
        <property name="configLocation" value="classpath:quartz.properties"/>
        <property name="triggers">
        	<list>
        		<ref bean="trigger1"/>
        		<ref bean="trigger2"/>
        	</list>
        </property>	
    </bean>
    <bean id="jobDetail1" class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="simpleService"/>
        <property name="targetMethod" value="testMethod1"/>
		<property name="shouldRecover" value="true"/>
    </bean>
    <bean id="trigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail1"/>
        <property name="cronExpression" value="0/5 * * ? * * *"/>
    </bean>    
  	
    <bean id="jobDetail2" class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="simpleService"/>
        <property name="targetMethod" value="testMethod2"/>
		<property name="shouldRecover" value="true"/>
    </bean>
    <bean id="trigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetail2"/>
        <property name="startDelay" value="1"/>
        <property name="repeatCount" value="100"/>
        <property name="repeatInterval" value="1000"/>
    </bean>
    
</beans>

5、 配置Job任务注意:加入定时任务有两种方式:
①     继承QuartzJobBean的类,重写executeInternal(),详细写法:
//类信息
public class SimpleService implements Serializable{
	
	private static final long serialVersionUID = 122323233244334343L;
	private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
	
	public void testMethod1(){
		logger.info("testMethod1.......1");
	}
	
	public void testMethod2(){
		logger.info("testMethod2.......2");	
	}
}
//配置信息
<bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailBean">

      <property name="jobClass">

            <value>继承QuartzJobBean的类的引用,如果不继承QuartzJobBean可以参考 http://www.javaeye.com/topic/486055</value>

      </property>

</bean>
 

②   用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean指定类和方法,但是直接使用会报java.io.NotSerializableException异常,一般用网上流传的(需要将两个类copy到自己的工程下,要有springJAR包,Job需要持久化到数据库中,SimpleService必须实现Serializable)frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean,可以参考: http://jira.springframework.org/browse/SPR-3797。详细写法:
<bean id="jobDetail1" class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="simpleService"/>
        <property name="targetMethod" value="testMethod1"/>
		<property name="shouldRecover" value="true"/>
    </bean>

6、启动测试
 ApplicationContext springContext = new
		 ClassPathXmlApplicationContext(new
		 String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});

启动服务后,spring quartz会将trigger持久化到数据库中
相关文档:http://dl.iteye.com/topics/download/fb709c8c-edab-3ed4-9898-e3e86deea379
参数网址:http://blog.csdn.net/lifetragedy/article/details/6212831
http://www.blogjava.net/freeman1984/archive/2012/05/11/377882.html

你可能感兴趣的:(spring,quartz,spring quartz集群)