Spring集成Quartz实现定时器

利用Spring集成Quartz可以轻松实现一个定时器,其中Quartz的配置applicationContext-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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
	default-lazy-init="true">

	<description>Quartz的本地Cron式执行任务配置</description>

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="autoStartup"><value>true</value></property>
		<property name="triggers">
			<list>
				<ref local="scheduleFactoryTrigger"/>
			</list>
		</property>
	</bean>
	
	<bean id="scheduleFactoryTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail"><ref bean="scheduleFactory" /></property>
		<property name="cronExpression">
			<value>0 35 10 * * ?</value>
		</property>
	</bean>
	
	<bean id="schedule" class="要执行的方法所在类的全路径"></bean>
	
	<bean id="scheduleFactory" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject"><ref bean="schedule" /></property>
		<property name="targetMethod"><value>execute</value></property><!--执行execute方法-->
		<property name="concurrent" value="false" />
	</bean>

</beans>

其中cronExpression可以根据自己的需要配置执行方法的时间,具体配置可以参考cronExpression语法详解。

然后需要在配置文件web.xml中添加对Quartz的引用,保证当服务器启动的时候,计时器就会生效。添加如下:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/applicationContext-quartz.xml,
		</param-value>
	</context-param>


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