如何正確的使用spring定時器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
公司主營業(yè)務:成都做網(wǎng)站、成都網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出新華免費做網(wǎng)站回饋大家。
第一種,使用XML配置的方法
前期工作,配置spring的開發(fā)環(huán)境(這里用到了spring的web應用包,需要導入)
首先創(chuàng)建定時器的任務類,定時器要做什么工作,就在這里寫什么方法。
package org.time; import java.util.TimerTask; public class MainTask extends TimerTask{ @Override public void run() { System.out.println("檢測用戶是否掉線"); } }
接著在配置文件中對定時器進行配置。
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="mainTask" class="org.time.MainTask"></bean> <!-- 注冊定時器信息 --> <bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <!-- 延遲1秒執(zhí)行首次任務 --> <property name="delay" value="1000"></property> <!-- 每隔2秒執(zhí)行一次任務 --> <property name="period" value="2000"></property> <!-- 具體執(zhí)行的任務 --> <property name="timerTask" ref="mainTask"></property> </bean> <!-- 配置任務調(diào)度器工廠 --> <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="springTask"/> </list> </property> </bean> </beans>
最后還需要在web.xml中對配置信息進行注冊:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> </web-app>
這樣就完成了定時器的配置,這時啟動tomcat,觀察控制臺輸出的結(jié)果:
第二種,使用注解的形式
(spring中一使用注解,感覺就是比其他方法方便了很多,代碼減少了很多)
這里需要用到AOP,需要引入AOP類庫
先看定時器的任務類:
package org.time; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MainTask01{ @Scheduled(cron = "0/3 * * * * ?") public void run() { System.out.println("推送消息來了"); } }
@Scheduled(cron = "0/3 * * * * ?") 表示三秒推送一次
corn可以配置各種時段任務:
字段 | 值 | 特殊表示字符 |
年 | 一般為空,1970-2099 | , - * / |
月 | 1-12 或者 JAN-DEC | , - * / |
星期 | 1-7 或者 SUN-SAT | , - * ? / L C # |
日 | 1-31 | , - * ? / L W C |
時 | 0-23 | , - * / |
分 | 0-59 | , - * / |
秒 | 0-59 | , - * / |
如: 配置每個工作日的10:20觸發(fā) :"0 20 10 ? * MON-FRI"
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <context:annotation-config /> <!-- spring掃描注解的配置 --> <context:component-scan base-package="org.time" /> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/> </beans>
配置文件的頭部信息中比上一個引入了
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
這兩句配置信息是必須要寫的,這是spring識別@Scheduled注解的關鍵
這這樣簡單的幾句配置之后,開啟服務,運行結(jié)果:
s
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
文章標題:如何正確的使用spring定時器
文章轉(zhuǎn)載:http://chinadenli.net/article46/gjsshg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、外貿(mào)網(wǎng)站建設、ChatGPT、、企業(yè)建站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)