quartz與spring的整合使用

maven依賴:spring

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.0.2</version>
</dependency>

Spring配置文件applicationContext-quartz.xml:app

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- 總管理類 若是將lazy-init='false'那麼容器啓動就會執行調度程序 -->
	<bean name="quartz_scheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="configLocation" value="classpath:quartz.properties" />
		<property name="triggers">
			<list>
				<ref bean="updateAccountTrigger" />
				<!-- other Trigger -->
			</list>
		</property>
	</bean>


	<!-- 定義調用對象和調用對象的方法 -->
	<bean id="updateAccountTask"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="sysTimerServiceImpl" />
		<property name="targetMethod" value="updateAllAccounts" />
		<property name="concurrent" value="false" />
	</bean>
	<!-- 9-19點每隔一個小時觸發 -->
	<bean id="updateAccountTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="updateAccountTask" />
		<property name="cronExpression" value="0 0 9-19 * * ?" />
	</bean>
	

</beans>
	

quartz.properties:	
	
	#============================================================================
	# Configure Main Scheduler Properties  
	#============================================================================
	org.quartz.scheduler.instanceName: quartz_scheduler_jobs
	org.quartz.scheduler.instanceId: AUTO
	org.quartz.scheduler.skipUpdateCheck: true
	#============================================================================
	# Configure ThreadPool  
	#============================================================================
	org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
	org.quartz.threadPool.threadCount: 5
	org.quartz.threadPool.threadPriority: 5

定時任務的內容:maven

// 調用對象
@Service("sysTimerServiceImpl")
public class SysTimerServiceImpl implements SysTimerService {

	// 調用方法
	[@Override](https://my.oschina.net/u/1162528)
	public void updateAllAccounts() {
		// ...
	}

}
相關文章
相關標籤/搜索