關(guān)于spring中定時(shí)器的使用教程
前言
在很多實(shí)際的web應(yīng)用中,都有需要定時(shí)實(shí)現(xiàn)的服務(wù),如每天12點(diǎn)推送個(gè)新聞,每隔一個(gè)小時(shí)提醒用戶休息一下眼睛,隔一段時(shí)間檢測(cè)用戶是否離線等等。
spring框架提供了對(duì)定時(shí)器的支持,通過(guò)配置文件就可以很好的實(shí)現(xiàn)定時(shí)器,只需要應(yīng)用啟動(dòng),就自動(dòng)啟動(dòng)定時(shí)器。下面介紹一下具體做法。
第一種,使用XML配置的方法
前期工作,配置spring的開(kāi)發(fā)環(huán)境(這里用到了spring的web應(yīng)用包,需要導(dǎo)入)
首先創(chuàng)建定時(shí)器的任務(wù)類,定時(shí)器要做什么工作,就在這里寫什么方法。
package org.time;
import java.util.TimerTask;
public class MainTask extends TimerTask{
@Override
public void run() {
System.out.println("檢測(cè)用戶是否掉線");
}
}
接著在配置文件中對(duì)定時(shí)器進(jìn)行配置。
<?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>
<!-- 注冊(cè)定時(shí)器信息 -->
<bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- 延遲1秒執(zhí)行首次任務(wù) -->
<property name="delay" value="1000"></property>
<!-- 每隔2秒執(zhí)行一次任務(wù) -->
<property name="period" value="2000"></property>
<!-- 具體執(zhí)行的任務(wù) -->
<property name="timerTask" ref="mainTask"></property>
</bean>
<!-- 配置任務(wù)調(diào)度器工廠 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="springTask"/>
</list>
</property>
</bean>
</beans>
最后還需要在web.xml中對(duì)配置信息進(jìn)行注冊(cè):
<?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>
這樣就完成了定時(shí)器的配置,這時(shí)啟動(dòng)tomcat,觀察控制臺(tái)輸出的結(jié)果:

第二種,使用注解的形式
(spring中一使用注解,感覺(jué)就是比其他方法方便了很多,代碼減少了很多)
這里需要用到AOP,需要引入AOP類庫(kù)
先看定時(shí)器的任務(wù)類:
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("推送消息來(lái)了");
}
}
@Scheduled(cron = "0/3 * * * * ?") 表示三秒推送一次
corn可以配置各種時(shí)段任務(wù):
|
字段 |
值 |
特殊表示字符 |
|
年 |
一般為空,1970-2099 |
, - * / |
|
月 |
1-12 或者 JAN-DEC |
, - * / |
|
星期 |
1-7 或者 SUN-SAT |
, - * ? / L C # |
|
日 |
1-31 |
, - * ? / L W C |
|
時(shí) |
0-23 |
, - * / |
|
分 |
0-59 |
, - * / |
|
秒 |
0-59 |
, - * / |
如: 配置每個(gè)工作日的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>
配置文件的頭部信息中比上一個(gè)引入了
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識(shí)別@Scheduled注解的關(guān)鍵
這這樣簡(jiǎn)單的幾句配置之后,開(kāi)啟服務(wù),運(yùn)行結(jié)果:
spring中使用注解的方法完成定時(shí)器,不需要集成其他父類定時(shí)器,使用簡(jiǎn)單方便!代碼量少,功能也很強(qiáng)大!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Springboot集成定時(shí)器和多線程異步處理操作
- Spring Boot定時(shí)器創(chuàng)建及使用解析
- Spring Boot 2 整合 QuartJob 實(shí)現(xiàn)定時(shí)器實(shí)時(shí)管理功能
- SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼
- SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
- 使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能
- 詳解spring batch的使用和定時(shí)器Quart的使用
- java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法
- Java中Spring使用Quartz任務(wù)調(diào)度定時(shí)器
- Spring整合Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)器的示例代碼
- JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
- 淺析spring定時(shí)器的使用
相關(guān)文章
Java通過(guò)MySQL的加解密函數(shù)實(shí)現(xiàn)敏感字段存儲(chǔ)
這篇文章主要介紹了如何在Java中MySQL的加解密函數(shù)實(shí)現(xiàn)敏感字段存儲(chǔ),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-03-03
java 數(shù)據(jù)結(jié)構(gòu)中棧結(jié)構(gòu)應(yīng)用的兩個(gè)實(shí)例
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)中棧結(jié)構(gòu)應(yīng)用的兩個(gè)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例
這篇文章主要介紹了Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例,包括一個(gè)反序的單鏈表結(jié)構(gòu)的例子,需要的朋友可以參考下2016-04-04
JavaGUI實(shí)現(xiàn)隨機(jī)單詞答題游戲
這篇文章主要為大家詳細(xì)介紹了JavaGUI實(shí)現(xiàn)隨機(jī)單詞答題游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
java Array和Arrays的區(qū)別總結(jié)
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java Array和Arrays的區(qū)別總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-03-03
基于bufferedreader的read()與readline()讀取出錯(cuò)原因及解決
這篇文章主要介紹了bufferedreader的read()與readline()讀取出錯(cuò)原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

