spring 对 timer 的支持---实现简单的web中定时器操作

spring 对 timer 的支持 实现简单的web中定时器操作
文章分类:Java编程

一共分两个步骤:

1.写一个java.util.Timer的子类,实现run方法。
Java代码

   1. package com.test; 
   2.  
   3. /**
   4.  * @author Evan
   5.  */ 
   6. public class TimerTaskSample extends java.util.TimerTask { 
   7.     @Override 
   8.     public void run() { 
   9.          
  10.         System.out.println("spring is invoking a timer task..."); 
  11.     } 
  12.  
  13. } 

package com.test;

/**
 * @author Evan
 */
public class TimerTaskSample extends java.util.TimerTask {
    @Override
    public void run() {
       
        System.out.println("spring is invoking a timer task...");
    }

}

 2.配置spring的配置文件。
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.     xmlns:aop="http://www.springframework.org/schema/aop
   5.     xmlns:tx="http://www.springframework.org/schema/tx
   6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx <a href="http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">http://www.springframework.org/schema/tx/spring-tx-2.0.xsd</a>">  
   7. <bean id="timerTask" class="com.test.TimerTaskSample"/> 
   8. <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
   9.     <property name="delay" value="10000"></property> 
  10.     <property name="period" value="5000"></property> 
  11.     <property name="timerTask" ref="timerTask" ></property> 
  12. </bean> 
  13.  
  14. <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
  15.     <property name="scheduledTimerTasks"> 
  16.     <list> 
  17.         <ref local="scheduledTask"></ref> 
  18.     </list> 
  19.     </property> 
  20. </bean> 
  21.  
  22. </beans> 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="timerTask" class="com.test.TimerTaskSample"/>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="delay" value="10000"></property>
    <property name="period" value="5000"></property>
    <property name="timerTask" ref="timerTask" ></property>
</bean>

<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
    <list>
        <ref local="scheduledTask"></ref>
    </list>
    </property>
</bean>

</beans>

 delay:延时(毫秒) ,过多久后执行timerTask

period:间隔多久(毫秒)执行一次

timerTask:要执行的任务。

 

timerFactory:spring调用任务的工厂,可以指定多个任务。

以上只是一个简单的例子,可以用于在启动web容器后,后台自动定时执行的内容可以增加在Timer子类的run方法中,比如:定时去查询数据库的数据,向其他服务器发消息等。

 

转自:http://blog.csdn.net/tammy_zhu/archive/2010/12/14/6075473.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值