在开发过程中,常常全有定时更新,定时数据拷贝和定时发邮件的功能.
Spring 中有关于定时器的功能,能很好地实现我们在开发中的需求.今天整理的是Timer.
1.编写BusinessTimer定时器
BusinessTimer
1import java.util.HashSet;
2import java.util.Set;
3import java.util.TimerTask;
4
5public class BusinessTimer extends TimerTask{
6
7 @Override
8 public void run() {
9 System.out.println(System.currentTimeMillis());
10 }
11}
1import java.util.HashSet;
2import java.util.Set;
3import java.util.TimerTask;
4
5public class BusinessTimer extends TimerTask{
6
7 @Override
8 public void run() {
9 System.out.println(System.currentTimeMillis());
10 }
11}
在BusinessTimer类中,实现所要触发的事务.
2.新建schedulingContext-timer.xml文件,在schedulingContext-timer.xml描述用户的定时器
schedulingContext-timer.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5 <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
6 <property name="scheduledTimerTasks">
7 <list>
8 <ref local="JorwangScheduledTimerTask1"/>
9 </list>
10 </property>
11 </bean>
12 <bean id="JorTimeTask1" class="BusinessTimer"></bean>
13 <bean id="JorwangScheduledTimerTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
14 <property name="timerTask"><ref local="JorTimeTask1"/></property>
15 <property name="delay"><value>10000</value></property>
16 <!-- 24小时启动-->
17 <property name="period"><value>86400000</value></property>
18 </bean>
19</beans>
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5 <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
6 <property name="scheduledTimerTasks">
7 <list>
8 <ref local="JorwangScheduledTimerTask1"/>
9 </list>
10 </property>
11 </bean>
12 <bean id="JorTimeTask1" class="BusinessTimer"></bean>
13 <bean id="JorwangScheduledTimerTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
14 <property name="timerTask"><ref local="JorTimeTask1"/></property>
15 <property name="delay"><value>10000</value></property>
16 <!-- 24小时启动-->
17 <property name="period"><value>86400000</value></property>
18 </bean>
19</beans>
3.在Web.xml中申明
web.xml
1.
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/schedulingContext-timer.xml</param-value>
5 </context-param>
6 <servlet>
7 <servlet-name>context</servlet-name>
8 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
9 <load-on-startup>1</load-on-startup>
10 </servlet>
11.
12
1.
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/schedulingContext-timer.xml</param-value>
5 </context-param>
6 <servlet>
7 <servlet-name>context</servlet-name>
8 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
9 <load-on-startup>1</load-on-startup>
10 </servlet>
11.
12
或者
web.xml
1.
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/schedulingContext-timer.xml</param-value>
5 </context-param>
6 <listener>
7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8 </listener>
9.
1.
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/schedulingContext-timer.xml</param-value>
5 </context-param>
6 <listener>
7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8 </listener>
9.