---
title: "Spring定时任务的多种使用方法总结 "
author: "ssxxjjii"
date: "2012-10-18T09:48:12Z"
tags: []
categories: ["互联网"]
slug: "spring"
draft: false
lastmod: "2012-10-18T09:48:12Z"
id: 1700724
url: "https://tdcq.iteye.com/blog/1700724"
---

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

 

**方法一：**

任务调度工作类代码：

Java代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

1. **public** **class** Clock **extends** TimerTask{
2. @Override
3. **public** **void** run() {
4. System.out.println("clock!");
5. }
6. }

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

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

Xml代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

01. **&lt;?xml** version="1.0" encoding="UTF-8"**?&gt;**
02. **&lt;beans** xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="http://www.springframework.org/schema/beans
05. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"**&gt;**
06. &lt;!-- 第一步 声明一个定时任务,该类extends java.util.TimerTask --&gt;
07. **&lt;bean** id="clock" class="com.Clock" **/&gt;**
08. &lt;!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 --&gt;
09. **&lt;bean** id="scheduledClock" class="org.springframework.scheduling.timer.ScheduledTimerTask"**&gt;**
10. **&lt;property** name="timerTask" ref="clock" **/&gt;**
11. &lt;!--这里是每隔多长时间就进行一次计时任务,单位ms--&gt;
12. **&lt;property** name="period"**&gt;**
13. **&lt;value&gt;**5000**&lt;/value&gt;**
14. **&lt;/property&gt;**
15. &lt;!--这里是服务启动后延时多少时间,开始计时任务,单位ms--&gt;
16. **&lt;property** name="delay"**&gt;**
17. **&lt;value&gt;**5000**&lt;/value&gt;**
18. **&lt;/property&gt;**
19. **&lt;/bean&gt;**
20. &lt;!-- 第三步 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 --&gt;
21. **&lt;bean** class="org.springframework.scheduling.timer.TimerFactoryBean"**&gt;**
22. **&lt;property** name="scheduledTimerTasks"**&gt;**
23. **&lt;list&gt;**
24. **&lt;ref** bean="scheduledClock" **/&gt;**
25. **&lt;/list&gt;**
26. **&lt;/property&gt;**
27. **&lt;/bean&gt;**
28. **&lt;/beans&gt;**

```
<?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> 
```

 

**方法二：**

任务调度工作类代码：

Java代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

1. **public** **class** QuartzClock **extends** QuartzJobBean {
2. @Override
3. **protected** **void** executeInternal(JobExecutionContext arg0) **throws** JobExecutionException {
4. System.out.println("QuartzClock!");
5. }
6. }

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

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

Xml代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

01. **&lt;?xml** version="1.0" encoding="UTF-8"**?&gt;**
02. **&lt;beans** xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="http://www.springframework.org/schema/beans
05. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"**&gt;**
06. &lt;!-- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 --&gt;
07. **&lt;bean** id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean"**&gt;**
08. **&lt;property** name="jobClass"**&gt;**
09. **&lt;value&gt;**com.QuartzClock**&lt;/value&gt;**
10. **&lt;/property&gt;**
11. **&lt;/bean&gt;**
12. &lt;!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数,配置方式同第一种方法 --&gt;
13. &lt;!--
14. **&lt;bean** id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean"**&gt;**
15. **&lt;property** name="jobDetail"**&gt;**
16. **&lt;ref** bean="quartzClock" **/&gt;**
17. **&lt;/property&gt;**
18. **&lt;property** name="startDelay"**&gt;**
19. **&lt;value&gt;**6000**&lt;/value&gt;**
20. **&lt;/property&gt;**
21. **&lt;property** name="repeatInterval"**&gt;**
22. **&lt;value&gt;**6000**&lt;/value&gt;**
23. **&lt;/property&gt;**
24. **&lt;/bean&gt;**
25. --**&gt;**
26. &lt;!-- 这种配置可以精确几点执行定时任务 --&gt;
27. **&lt;bean** id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean"**&gt;**
28. **&lt;property** name="jobDetail" ref="quartzClock" **/&gt;**
29. **&lt;property** name="cronExpression"**&gt;**
30. &lt;!--   0/15 * * * * ? 每15秒钟  --&gt;
31. **&lt;value&gt;**0/15 * * * * ?**&lt;/value&gt;**
32. **&lt;/property&gt;**
33. **&lt;/bean&gt;**
34. &lt;!--第三步 启动定时任务,注意这里的ref bean --&gt;
35. **&lt;bean** class="org.springframework.scheduling.quartz.SchedulerFactoryBean"**&gt;**
36. **&lt;property** name="triggers"**&gt;**
37. **&lt;list&gt;**
38. **&lt;ref** bean="cronQuartzClock" **/&gt;**
39. **&lt;/list&gt;**
40. **&lt;/property&gt;**
41. **&lt;/bean&gt;**
42. **&lt;/beans&gt;**

```
<?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>  
```

 

**方法三：**

任务调度工作类代码：

Java代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

1. **public** **class** TaskServiceImpl{
2. **public** **void** synchronizeDb(){
3. System.out.println("Quartz的任务调度!");
4. }
5. }

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

 

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

Xml代码 [![复制代码](http://tdcq.iteye.com/images/icon_copy.gif)](http://tdcq.iteye.com/admin/blogs/ "复制代码") [![收藏代码](http://tdcq.iteye.com/images/icon_star.png)![](http://tdcq.iteye.com/images/spinner.gif)]()

01. **&lt;?xml** version="1.0" encoding="UTF-8"**?&gt;**
02. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"**&gt;**
03. **&lt;beans&gt;**
04. &lt;!-- 定时任务的工作Bean --&gt;
05. **&lt;bean** id="quartzJob" class="com.whty.task.service.impl.TaskServiceImpl" **/&gt;**
06. &lt;!-- 定义生成工作对象的工厂，并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 --&gt;
07. **&lt;bean** id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"**&gt;**
08. **&lt;property** name="targetObject" ref="quartzJob" **/&gt;**
09. **&lt;property** name="targetMethod"**&gt;**
10. **&lt;value&gt;**synchronizeDb**&lt;/value&gt;**
11. **&lt;/property&gt;**
12. **&lt;property** name="concurrent" value="false" **/&gt;**
13. **&lt;/bean&gt;**
14. &lt;!-- 任务调度计时器，进行定时设置。CronTriggerBean能进行非常精确的定时设置 --&gt;
15. **&lt;bean** id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean"**&gt;**
16. **&lt;property** name="jobDetail" ref="jobDetail" **/&gt;**
17. &lt;!-- cron表达式 --&gt;
18. **&lt;property** name="cronExpression"**&gt;**
19. &lt;!--   0 0 \*/2 * * ? 每两小时、整点触发 --&gt;
20. &lt;!--   0 0/2 * * * ? 每两分钟  --&gt;
21. &lt;!--   0/5 * * * * ? 每五秒钟  --&gt;
22. &lt;!--   0 15 10 * * ? 每天Y分X点触发  --&gt;
23. **&lt;value&gt;**0/15 * * * * ?**&lt;/value&gt;**
24. **&lt;/property&gt;**
25. **&lt;/bean&gt;**
26. &lt;!-- 调度任务触发器，启动定时任务--&gt;
27. **&lt;bean** autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"**&gt;**
28. **&lt;property** name="triggers"**&gt;**
29. **&lt;list&gt;**
30. **&lt;ref** bean="cronQuartzClock" **/&gt;**
31. **&lt;/list&gt;**
32. **&lt;/property&gt;**
33. **&lt;/bean&gt;**
34. **&lt;/beans&gt;**

```
<?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 每个工作日的工作时间  
\- 区间  
\* 通配符  
? 你不想设置那个字段

 

[http://kingxss.iteye.com/blog/1620383](http://kingxss.iteye.com/blog/1620383)