有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz。
1.Java Timer定时
首先继承java.util.TimerTask类实现run方法
import java.util.TimerTask;
public class EmailReportTask extends TimerTask{
@Override
public void run() {
...
}
}
在Spring定义
...
配置Spring定时器
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask" />
<property name="period">
<value>86400000value>
property>
bean>
timerTask属性告诉ScheduledTimerTask运行哪个。86400000代表24个小时
启动Spring定时器
Spring的TimerFactoryBean负责启动定时任务
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list><ref bean="scheduleReportTask"/>list>
property>
bean>
scheduledTimerTasks里显示一个需要启动的定时器任务的列表。
可以通过设置delay属性延迟启动
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask" />
<property name="period">
<value>86400000value>
property>
<property name="delay">
<value>3600000value>
property>
bean>
这个任务我们只能规定每隔24小时运行一次,无法精确到某时启动
2.Quartz定时器
首先继承QuartzJobBean类实现executeInternal方法
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailReportJob extends QuartzJobBean{
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
...
}
}
在Spring中定义
<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>EmailReportJobvalue>
property>
<property name="jobDataAsMap">
<map>
1.Java Timer定时
首先继承java.util.TimerTask类实现run方法
import java.util.TimerTask;
public class EmailReportTask extends TimerTask{
@Override
public void run() {
...
}
}
在Spring定义
...
配置Spring定时器
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask" />
<property name="period">
<value>86400000value>
property>
bean>
timerTask属性告诉ScheduledTimerTask运行哪个。86400000代表24个小时
启动Spring定时器
Spring的TimerFactoryBean负责启动定时任务
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list><ref bean="scheduleReportTask"/>list>
property>
bean>
scheduledTimerTasks里显示一个需要启动的定时器任务的列表。
可以通过设置delay属性延迟启动
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="reportTimerTask" />
<property name="period">
<value>86400000value>
property>
<property name="delay">
<value>3600000value>
property>
bean>
这个任务我们只能规定每隔24小时运行一次,无法精确到某时启动
2.Quartz定时器
首先继承QuartzJobBean类实现executeInternal方法
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailReportJob extends QuartzJobBean{
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
...
}
}
在Spring中定义
<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>EmailReportJobvalue>
property>
<property name="jobDataAsMap">
<map>
![](http://hiphotos.baidu.com/zhangsuile/pic/item/d4607bede81a9edab21cb128.jpg?v=tbs)