`
kingxss
  • 浏览: 969332 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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>

 

特别说明:

0 0/2 * * * ? 每两分钟

就上面的这个cron表达式,定时任务在第三种配置模式下,表现为以下行为:

定时任务是一个线程在执行,例如线程在09:00:00启动,且完成工作在09:02:00 之前,那么线程健在09:02:00再次启动。如果线程在09:00:00启动,且完成工作在09:02:00之后,那么线程健在完成时间再次启动。

 

在该配置下,总结规律如下:

1.定时任务将由一个线程启动,而不是多个线程执行。

2.该线程每两分钟启动一次,该时间间隔是线程每次启动的时间差,如果线程执行时间超出了两分钟的设定间隔,那么线程在执行完成后继续执行。

 

多线线程配置模式下的运行规律大家可以自行测试。

 

附录:

Cron 表达式详解和案例 

分享到:
评论
1 楼 冰影轮 2014-05-08  
请问 怎么通过jaa程序 停止这个定时任务呢?

相关推荐

Global site tag (gtag.js) - Google Analytics