Spring framework实现定时器之Timer

    在开发过程中,常常全有定时更新,定时数据拷贝和定时发邮件的功能.

    Spring 中有关于定时器的功能,能很好地实现我们在开发中的需求.今天整理的是Timer.

    1.编写BusinessTimer定时器

ContractedBlock.gif ExpandedBlockStart.gif BusinessTimer
 1import java.util.HashSet;
 2import java.util.Set;
 3import java.util.TimerTask;
 4
 5ExpandedBlockStart.gifContractedBlock.gifpublic class BusinessTimer extends TimerTask{
 6
 7    @Override
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    public void run() {
 9    System.out.println(System.currentTimeMillis());           
10    }

11}

      在BusinessTimer类中,实现所要触发的事务.

  2.新建schedulingContext-timer.xml文件,在schedulingContext-timer.xml描述用户的定时器

ContractedBlock.gif ExpandedBlockStart.gif 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>

 

  3.在Web.xml中申明

ContractedBlock.gif ExpandedBlockStart.gif 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

     或者

ContractedBlock.gif ExpandedBlockStart.gif 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.

 

   

转载于:https://www.cnblogs.com/JavaVillage/archive/2009/02/13/1389988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值