spring 對 timer 的支援 實現簡單的web中定時器操作

來源:互聯網
上載者:User

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方法中,比如:定時去查詢資料庫的資料,向其他伺服器發訊息等。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.