生活随笔
收集整理的這篇文章主要介紹了
在Spring中使用JDK定时器实现调度任务
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Spring中使用JDK定時(shí)器實(shí)現(xiàn)調(diào)度任務(wù) 作者:chszs,轉(zhuǎn)載需注明。博客主頁(yè): http://blog.csdn.net/chszs 本文探討Spring如何集成JDK的Timer定時(shí)器,實(shí)現(xiàn)計(jì)劃執(zhí)行任務(wù)。 有時(shí)候,需要執(zhí)行一些無(wú)用戶(hù)交互的程序,就像在指定的時(shí)間間隔后臺(tái)運(yùn)行進(jìn)程那樣。比如,殺毒軟件可以每隔2天就在后臺(tái)運(yùn)行一次。又比如某些程序每天都要連接一次服務(wù)器,查看有沒(méi)有更新。 本文探討Spring如何集成JDK的Timer定時(shí)器,實(shí)現(xiàn)計(jì)劃執(zhí)行任務(wù)。
一、Spring框架集成JDK的Timer JDK的Timer任務(wù)對(duì)象提供了在指定時(shí)間執(zhí)行任何任務(wù)的功能。我們來(lái)看下面的例子: 假設(shè)我們想寫(xiě)一個(gè)服務(wù),此服務(wù)周期性的檢查互聯(lián)網(wǎng)連接,并用日志記錄連接的狀態(tài)。讓我們假定此服務(wù)是全天候運(yùn)行的,且每隔30秒執(zhí)行一次。 所需要的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
二、寫(xiě)服務(wù)類(lèi) 下面的類(lèi)實(shí)現(xiàn)了互聯(lián)網(wǎng)連接檢查。 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;}}
}
上面的代碼很簡(jiǎn)單,doCheck()方法檢查互聯(lián)網(wǎng)連接是否有效。
三、封裝定時(shí)器任務(wù)服務(wù) 下面我們寫(xiě)一個(gè)服務(wù),實(shí)現(xiàn)定時(shí)任務(wù)。代碼如下: 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();}
}
此類(lèi)繼承了java.util.TimerTask類(lèi)。 重寫(xiě)了run()方法,可以執(zhí)行任意操作。這里是調(diào)用互聯(lián)網(wǎng)連接檢查。 注意定時(shí)器任務(wù)依賴(lài)于連接服務(wù)對(duì)象。稍后,我們將看到怎樣連線(xiàn)這兩個(gè)對(duì)象。
四、配置 至今,我們還沒(méi)有指定執(zhí)行的時(shí)間間隔。Spring提供了這樣的配置支持。下面我們來(lái)看看該如何配置: 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>
以上配置文件的細(xì)節(jié): "connectionCheckService"這個(gè)Bean表示互聯(lián)網(wǎng)連接服務(wù)。 "connectionCheckTimerTask"這個(gè)Bean定義了定時(shí)器任務(wù)。由于此定時(shí)器任務(wù)依賴(lài)于"connectionCheckService"這個(gè)Bean,故通過(guò)配置進(jìn)行注入。 下面的代碼是從Spring框架中聲明定時(shí)器任務(wù)的調(diào)度對(duì)象:
<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這個(gè)類(lèi)提供了對(duì)定時(shí)任務(wù)調(diào)度執(zhí)行的支持。 屬性delay的單位是毫秒,它指定任務(wù)執(zhí)行前需要延時(shí)多少時(shí)間。2000意味著延時(shí)2秒開(kāi)始執(zhí)行任務(wù)。 第二個(gè)屬性period的單位也是毫秒,它表示任務(wù)每隔多少時(shí)間就重復(fù)執(zhí)行一次。30000這個(gè)值表示每隔30秒執(zhí)行一次。 最后一個(gè)屬性是timerTask,它指定實(shí)際要執(zhí)行的任務(wù)。 觸發(fā)調(diào)度任務(wù)是通過(guò)TimerFactoryBean進(jìn)行的。它可以指定待調(diào)度的任務(wù)對(duì)象列表,盡管這里只有1個(gè)待調(diào)度的任務(wù)對(duì)象。
<bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduledConnectionCheckTimerTask" /></list></property></bean>
?
五、客戶(hù)端 客戶(hù)端程序會(huì)載入應(yīng)用程序的上下文。一旦上下文被載入,服務(wù)對(duì)象、定時(shí)器任務(wù)對(duì)象、調(diào)度的定時(shí)器任務(wù)對(duì)象都會(huì)被載入并連線(xiàn)。下面我們繼續(xù)介紹觸發(fā)器Bean是如何觸發(fā)定時(shí)器任務(wù)的執(zhí)行,互聯(lián)網(wǎng)連接在每隔30秒運(yùn)行一次。 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");}
}
運(yùn)行Client.java,可以看到每隔30秒定時(shí)器任務(wù)就調(diào)度執(zhí)行一次。 執(zhí)行結(jié)果如下:
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
轉(zhuǎn)載于:https://www.cnblogs.com/pangblog/p/3253667.html
總結(jié)
以上是生活随笔 為你收集整理的在Spring中使用JDK定时器实现调度任务 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。