`

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系统调度执行。至此,所有的配置已完成,任务已能正常跑了。

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics