亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于spring中定時器的使用教程

 更新時間:2017年06月23日 11:04:06   作者:阿木俠  
大家應該都有所體會,在很多實際的web應用中,都有需要定時實現(xiàn)的服務,下面這篇文章主要給大家介紹了關(guān)于spring中定時器的使用教程,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

在很多實際的web應用中,都有需要定時實現(xiàn)的服務,如每天12點推送個新聞,每隔一個小時提醒用戶休息一下眼睛,隔一段時間檢測用戶是否離線等等。

spring框架提供了對定時器的支持,通過配置文件就可以很好的實現(xiàn)定時器,只需要應用啟動,就自動啟動定時器。下面介紹一下具體做法。

第一種,使用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注解的關(guān)鍵

這這樣簡單的幾句配置之后,開啟服務,運行結(jié)果:

 

spring中使用注解的方法完成定時器,不需要集成其他父類定時器,使用簡單方便!代碼量少,功能也很強大!

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Java中Spock框架Mock對象的方法經(jīng)驗總結(jié)

    Java中Spock框架Mock對象的方法經(jīng)驗總結(jié)

    這篇文章主要分享了Spock框架Mock對象的方法經(jīng)驗總結(jié),下文分享一些常用項目實戰(zhàn)說明以及代碼,供大家項目中參考,也具有一的的參考價值,需要的小伙伴可以參考一下
    2022-02-02
  • SpringCloud Config分布式配置中心使用教程介紹

    SpringCloud Config分布式配置中心使用教程介紹

    springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應用
    2022-12-12
  • 淺析Spring的JdbcTemplate方法

    淺析Spring的JdbcTemplate方法

    本篇淺析Spring的JdbcTemplate方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java Socket實現(xiàn)UDP編程淺析

    Java Socket實現(xiàn)UDP編程淺析

    類 DatagramSocket 何 DatagramPacket(數(shù)據(jù)包/數(shù)據(jù)報) 實現(xiàn)了基于 UDP協(xié)議網(wǎng)絡程序;UDP數(shù)據(jù)報通過數(shù)據(jù)報套接字 DatagramSocket 發(fā)送和接收,系統(tǒng)不保證 UDP數(shù)據(jù)報一定能夠安全送達目的地,也不確定什么時候可以抵達
    2022-11-11
  • MyBatis-Plus?條件查詢器的實現(xiàn)

    MyBatis-Plus?條件查詢器的實現(xiàn)

    本文主要介紹了MyBatis-Plus?條件查詢器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • 淺談Spring中如何使用設(shè)計模式

    淺談Spring中如何使用設(shè)計模式

    這篇文章主要介紹了淺談Spring中如何使用設(shè)計模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • mybatis配置文件簡介_動力節(jié)點Java學院整理

    mybatis配置文件簡介_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了mybatis配置文件簡介的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Java實現(xiàn)批量修改txt文件名稱的方法示例

    Java實現(xiàn)批量修改txt文件名稱的方法示例

    這篇文章主要介紹了Java實現(xiàn)批量修改txt文件名稱的方法,結(jié)合實例形式分析了Java針對目錄文件遍歷及文件讀寫、屬性操作等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • Jenkins環(huán)境搭建實現(xiàn)過程圖解

    Jenkins環(huán)境搭建實現(xiàn)過程圖解

    這篇文章主要介紹了Jenkins環(huán)境搭建實現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot整合MongoDB全過程

    SpringBoot整合MongoDB全過程

    這篇文章主要介紹了SpringBoot整合MongoDB全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論