Spring quartz集羣配置

quartz單機模式:http://blog.csdn.net/itjavaer/article/details/77923965


quartz集羣要依賴數據庫的,不同版本表可能會不一樣,建表語句在下載的安裝包裏的docs\dbTables,都是qrtz_開頭的


建完表之後看下qrtz_locks表,這個表裏有沒有數據,如果沒有就手動加上,不然可能報錯

insert into `qrtz_locks` (`LOCK_NAME`) values('CALENDAR_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('JOB_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('MISFIRE_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('STATE_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('TRIGGER_ACCESS');


創建集羣的配置文件 quartz.properties

org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
 

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
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.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = qrtz_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

org.quartz.threadPool.threadCount = 1    線程設置成1是爲了保證不併發執行,單機模式下設置只要設置<property name="concurrent" value="false" />就行了,但是集羣不能設置這個屬性,如果有更好的辦法請告訴我。


創建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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"  
 	xmlns:ws="http://jax-ws.dev.java.net/spring/core" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-4.0.xsd   
          http://www.springframework.org/schema/aop   
          http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
          http://www.springframework.org/schema/tx    
          http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
          http://jax-ws.dev.java.net/spring/core
          http://jax-ws.dev.java.net/spring/core.xsd">
          
    

	<bean name="testTask" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="name" value="testTask"></property>  
        <property name="jobClass" value="com.orange.task.TestTask"></property>  
	</bean>  
	
	<bean id="testTriggerBean"  class="org.springframework.scheduling.quartz.CronTriggerBean">  
	    <property name="jobDetail">  
	        <ref bean="testTask" />  
	    </property>  

	    <property name="cronExpression">  
	        <value>0 * * * * ?</value>  
	    </property>  
	</bean>  
	    
    <bean id = "quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
	            <ref bean="testTriggerBean" /> 
            </list>  
        </property>  
         <property name="configLocation" value="classpath:quartz.properties" />  
         <property name="dataSource" ref ="dataSourceQuartz" />
    </bean>  
</beans>

id = "quartzScheduler" 要和quartz.properties文件中的org.quartz.scheduler.instanceName 一樣

配置數據源,我是寫在了spring-mybatis.xml

<bean name="dataSourceQuartz" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="jdbc:mysql://localhost:3306/quartz?characterEncoding=UTF-8" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialSize" value="10" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="20" />
		<property name="minIdle" value="1" />
		<property name="maxWait" value="60000" />
		<property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="10000" />
		<property name="removeAbandoned" value="false" />
		<property name="removeAbandonedTimeout" value="1800" />
		<property name="logAbandoned" value="true" />
	</bean> 

 

最後在spring.xml引入spring-quartz.xml

<import resource="classpath:spring-quartz.xml"/>

創建任務類,要實現Job接口

package com.orange.task;  
import com.orange.service.TestService;  
  
public class TestTask implements Job{  
    @Autowired  
    private TestService testService;  
     
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {  
        System.out.println(date);  
        testService.test(date);  
    }  
}  


啓動兩次程序,兩個程序不會同時運行一個任務(可能交替,也能連續),一個程序掛了,另一個程序會繼續運行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章