生活随笔
收集整理的這篇文章主要介紹了
在Spring中使用JDK定时器实现调度任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Spring中使用JDK定時器實現調度任務 作者:chszs,轉載需注明。博客主頁: http://blog.csdn.net/chszs 本文探討Spring如何集成JDK的Timer定時器,實現計劃執行任務。 有時候,需要執行一些無用戶交互的程序,就像在指定的時間間隔后臺運行進程那樣。比如,殺毒軟件可以每隔2天就在后臺運行一次。又比如某些程序每天都要連接一次服務器,查看有沒有更新。 本文探討Spring如何集成JDK的Timer定時器,實現計劃執行任務。
一、Spring框架集成JDK的Timer JDK的Timer任務對象提供了在指定時間執行任何任務的功能。我們來看下面的例子: 假設我們想寫一個服務,此服務周期性的檢查互聯網連接,并用日志記錄連接的狀態。讓我們假定此服務是全天候運行的,且每隔30秒執行一次。 所需要的JAR包如下: log4j-1.2.13.jar commons-logging-1.1.1.jar spring-beans-3.2.4.RELEASE.jar spring-context-3.2.4.RELEASE.jar spring-context-support-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-expression-3.2.4.RELEASE.jar
二、寫服務類 下面的類實現了互聯網連接檢查。 Listing 1: CheckInternetConnectionService.java
package com.chszs;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;public class CheckInternetConnectionService {public void checkConnection(){if(doCheck()){System.out.println(new Date() + "Internet connection available");}else{System.out.println(new Date() + "Internet connection not available");}}private boolean doCheck(){URL urlObject = null;URLConnection urlConnection = null;try{urlObject = new URL("http://www.baidu.com");urlConnection = urlObject.openConnection();urlConnection.getContent();return true;}catch(Exception e){return false;}}
}
上面的代碼很簡單,doCheck()方法檢查互聯網連接是否有效。
三、封裝定時器任務服務 下面我們寫一個服務,實現定時任務。代碼如下: Listing 2: CheckInternetConnectionWithTimerTask
package com.chszs;
import java.util.TimerTask;public class CheckInternetConnectionWithTimerTask extends TimerTask{private CheckInternetConnectionService service;public CheckInternetConnectionService getService(){return service;}public void setService(CheckInternetConnectionService service){this.service = service;}@Overridepublic void run() {service.checkConnection();}
}
此類繼承了java.util.TimerTask類。 重寫了run()方法,可以執行任意操作。這里是調用互聯網連接檢查。 注意定時器任務依賴于連接服務對象。稍后,我們將看到怎樣連線這兩個對象。
四、配置 至今,我們還沒有指定執行的時間間隔。Spring提供了這樣的配置支持。下面我們來看看該如何配置: Listing 3: timer.xml
<?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:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="connectionCheckService" class="com.chszs.CheckInternetConnectionService"></bean><bean id="connectionCheckTimerTask" class="com.chszs.CheckInternetConnectionWithTimerTask"><property name="service" ref="connectionCheckService" /></bean><bean id="scheduledConnectionCheckTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="delay" value="2000" /><property name="period" value="30000" /><property name="timerTask" ref="connectionCheckTimerTask" /></bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduledConnectionCheckTimerTask" /></list></property></bean>
</beans>
以上配置文件的細節: "connectionCheckService"這個Bean表示互聯網連接服務。 "connectionCheckTimerTask"這個Bean定義了定時器任務。由于此定時器任務依賴于"connectionCheckService"這個Bean,故通過配置進行注入。 下面的代碼是從Spring框架中聲明定時器任務的調度對象:
<bean id="scheduledConnectionCheckTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="delay" value="2000" /><property name="period" value="30000" /><property name="timerTask" ref="connectionCheckTimerTask" /></bean>
org.springframework.scheduling.timer.ScheduledTimerTask這個類提供了對定時任務調度執行的支持。 屬性delay的單位是毫秒,它指定任務執行前需要延時多少時間。2000意味著延時2秒開始執行任務。 第二個屬性period的單位也是毫秒,它表示任務每隔多少時間就重復執行一次。30000這個值表示每隔30秒執行一次。 最后一個屬性是timerTask,它指定實際要執行的任務。 觸發調度任務是通過TimerFactoryBean進行的。它可以指定待調度的任務對象列表,盡管這里只有1個待調度的任務對象。
<bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduledConnectionCheckTimerTask" /></list></property></bean>
?
五、客戶端 客戶端程序會載入應用程序的上下文。一旦上下文被載入,服務對象、定時器任務對象、調度的定時器任務對象都會被載入并連線。下面我們繼續介紹觸發器Bean是如何觸發定時器任務的執行,互聯網連接在每隔30秒運行一次。 Listing 4: Client.java
package com.chszs;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("timer.xml");}
}
運行Client.java,可以看到每隔30秒定時器任務就調度執行一次。 執行結果如下:
Sun Aug 11 21:08:26 CST 2013Internet connection available
Sun Aug 11 21:08:56 CST 2013Internet connection available
Sun Aug 11 21:09:26 CST 2013Internet connection available
Sun Aug 11 21:09:56 CST 2013Internet connection available
轉載于:https://www.cnblogs.com/pangblog/p/3253667.html
總結
以上是生活随笔 為你收集整理的在Spring中使用JDK定时器实现调度任务 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。