spring +quartz 配置

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
	
	
	<!-- 要調用的工作類 -->
	<bean id="synchronizeGameInfo" class="com.cyou.gamehistory.trigger.SynchronizeGameInfo">
	</bean>
	 <!-- 定義調用對象和調用對象的方法 -->
	<bean id="updateGameInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="synchronizeGameInfo" />
		<property name="targetMethod" value="synchronizeGame" />
		<!-- 非併發 -->
		<property name="concurrent" value="false" /> 
	</bean>
	<!-- 定義觸發時間 -->
	<bean id="updateGameTrigger" lazy-init="false" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="updateGameInfo"></property>
		<property name="cronExpression">  
            <!-- 每週星期天凌晨1點實行一次 -->  
            <value>0 0 1 ? * L</value>  
        </property>  
	</bean>
	<!-- 總管理類 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="updateGameTrigger"/>
			</list>
		</property>
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.threadCount">1</prop>
			</props>
		</property>
	</bean>
	
</beans>



 

 

第一步,在Spring配置文件中增加本業務類

<bean id="synchronizeGameInfo" class="com.cyou.gamehistory.trigger.SynchronizeGameInfo">


第二步,定義任務。在Spring配置文件中配置代理類MethodInvokingJobDetailFactoryBean,定義任務的詳細信息。

<bean id="updateGameInfo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="synchronizeGameInfo" />
		<property name="targetMethod" value="synchronizeGame" />
		<!-- 非併發 -->
		<property name="concurrent" value="false" /> 
</bean>



這個配置告訴Spring,我們的任務是執行id爲synchronizeGameInfo的bean中的synchronizeGame函數。其中參數concurrent告訴Spring,不要併發運行這個任務。

第三步,配置一個觸發器。在Spring配置文件中配置觸發器類updateGameTrigger。

 

<bean id="updateGameTrigger" lazy-init="false" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="updateGameInfo"></property>
		<property name="cronExpression">  
            <!-- 每週星期天凌晨1點實行一次 -->  
            <value>0 0 1 ? * L</value>  
        </property>  
	</bean>


觸發器將告訴Quartz兩件事:在何時觸發任務、觸發哪個任務。其中屬性參數cronExpression爲調度時間,格式和unix上的crontab類似,其中問號表示忽略該位置(星期)上的值。屬性參數jobDetail指向具體的任務bean:updateGameInfo 。如果你有多個任務,每個任務的觸發時間都不一樣,則你可以在此配置多個不同的觸發器。

 

 

第四步,配置一個調度器。在Spring配置文件中配置調度器類SchedulerFactoryBean。

 

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="updateGameTrigger"/>
			</list>
		</property>
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.threadCount">1</prop>
			</props>
		</property>
	</bean>


該調度器用於管理觸發器。只有在調度器中列表出現的觸發器才被Quartz系統調度執行。至此,所有的配置已完成,任務已能正常跑了。

 

 

 

 

 

發佈了35 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章