Spring定时任务的多种使用方法小结

Spring定时任务的多种使用方法总结

这里使用的是Spring2.5,需要的jar包:spring.jar(spring2.5的完全包);quartz-all-1.6.0.jar;还需commons-*.jar。

 

方法一:

任务调度工作类代码:

public class Clock extends TimerTask{       
	@Override   
	public void run() {          
	   System.out.println("clock!");     
	}      
}  

应用上下文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-2.0.xsd">
	<!-- 第一步 声明一个定时任务,该类extends java.util.TimerTask --> 
	<bean id="clock" class="com.Clock" />
	<!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 --> 
	<bean id="scheduledClock" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="timerTask" ref="clock" />
		<!--这里是每隔多长时间就进行一次计时任务,单位ms--> 
		<property name="period"> 
			<value>5000</value> 	
		</property>
		<!--这里是服务启动后延时多少时间,开始计时任务,单位ms--> 
		<property name="delay"> 
			<value>5000</value> 
		</property> 
	</bean>

	<!-- 第三步 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 --> 
	<bean class="org.springframework.scheduling.timer.TimerFactoryBean"> 
		<property name="scheduledTimerTasks"> 
			<list> 
				<ref bean="scheduledClock" />
			</list> 
		</property> 
	</bean>
</beans> 
 

方法二:

任务调度工作类代码:

public class QuartzClock extends QuartzJobBean {    
	@Override   
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {    
	   System.out.println("QuartzClock!");    
	}     
}  

应用上下文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-2.0.xsd">
	<!-- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 --> 
	<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass"> 
			<value>com.QuartzClock</value> 
		</property> 
	</bean>
	
	<!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数,配置方式同第一种方法 --> 
	<!-- 
	<bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail"> 
			<ref bean="quartzClock" /> 
		</property>  
		<property name="startDelay"> 
			<value>6000</value> 
		</property> 
		<property name="repeatInterval"> 
			<value>6000</value> 
		</property> 
	</bean> 
	-->

	<!-- 这种配置可以精确几点执行定时任务 --> 
	<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="quartzClock" />
		<property name="cronExpression"> 
			<!--   0/15 * * * * ? 每15秒钟  -->
			<value>0/15 * * * * ?</value>
		</property> 
	</bean> 
	
	<!--第三步 启动定时任务,注意这里的ref bean --> 
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
		<property name="triggers"> 
			<list> 
				<ref bean="cronQuartzClock" />
			</list> 
		</property> 
	</bean> 
</beans>  

 

方法三:

任务调度工作类代码:

public class TaskServiceImpl{
	public void synchronizeDb(){
		System.out.println("Quartz的任务调度!"); 
	}
}
 

应用上下文XML中的具体配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>	
	<!-- 定时任务的工作Bean -->
	<bean id="quartzJob" class="com.whty.task.service.impl.TaskServiceImpl" />
	
	<!-- 定义生成工作对象的工厂,并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 -->
	<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="quartzJob" />
		<property name="targetMethod">
			<value>synchronizeDb</value>
		</property>
		<property name="concurrent" value="false" />
	</bean>
	
	<!-- 任务调度计时器,进行定时设置。CronTriggerBean能进行非常精确的定时设置 -->
	<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobDetail" />
		<!-- cron表达式 -->
		<property name="cronExpression">
			<!--   0 0 */2 * * ? 每两小时、整点触发 -->
			<!--   0 0/2 * * * ? 每两分钟  -->
			<!--   0/5 * * * * ? 每五秒钟  -->
			<!--   0 15 10 * * ? 每天Y分X点触发  -->
			<value>0/15 * * * * ?</value>
		</property>
	</bean>
	
	<!-- 调度任务触发器,启动定时任务-->
	<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="cronQuartzClock" />
			</list>
		</property>
	</bean>
</beans>

 

附录:cron表达式详解

 

一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:
1.秒(0-59)
2.分钟(0-59)
3.小时(0-23)
4.月份中的是期(1-31)
5.月份(1-12或SUN-DEC)
6.星期中的日期(1-7或SUN-SAT)
7.年份(1970-2099)
例子:
0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点
0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟
30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时
0 0 8-5 ? * MON-FRI 每个工作日的工作时间
- 区间
* 通配符
? 你不想设置那个字段