Spring中@Scheduled功能的使用方法詳解
前言
Spring 為任務(wù)調(diào)度和基于使用@Scheduled 注釋的 cron 表達(dá)式的異步方法執(zhí)行提供了極好的支持??梢詫Scheduled 注釋與觸發(fā)器元數(shù)據(jù)一起添加到方法中。在這篇文章中,我將以4種不同的方式展示@Scheduled 功能的使用方法。
一、Spring @Scheduled Annotation
@ scheduled注釋用于任務(wù)調(diào)度。觸發(fā)器信息需要與這個(gè)注釋一起提供。
您可以使用屬性 fixedDelay/fixedRate/cron 來提供觸發(fā)信息。
- fixedRate 使 Spring 定期運(yùn)行任務(wù),即使最后一次調(diào)用仍在運(yùn)行
- fixedDelay 特別控制最后一次執(zhí)行結(jié)束時(shí)的下一次執(zhí)行時(shí)間。
- Cron 是一個(gè)源自 Unix cron 實(shí)用工具的特性,并且根據(jù)您的需求有各種選項(xiàng)。
示例用法如下:
@Scheduled Usages @Scheduled(fixedDelay =30000) public void demoServiceMethod () {... } @Scheduled(fixedRate=30000) public void demoServiceMethod () {... } @Scheduled(cron="0 0 * * * *") public void demoServiceMethod () {... }
1.2 如何啟用@Scheduled 注釋
要在 spring 應(yīng)用程序中使用@Scheduled,必須首先在 applicationConfig.xml 文件中定義 xml 名稱空間和模式位置定義。還添加任務(wù): 注釋驅(qū)動(dòng),以支持基于注釋的任務(wù)調(diào)度。
applicationConfig.xml xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven>
上面的添加是必要的,因?yàn)槲覀儗⑹褂没谧⑨尩呐渲谩?/p>
1.3 使用@Scheduled 注釋
下一步是在類中創(chuàng)建一個(gè)類和一個(gè)方法,如下所示:
DemoService.java public class DemoService { @Scheduled(cron="*/5 * * * * ?") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } }
在上面的例子中
- 使用@Scheduled 注釋反過來會(huì)使 Spring 容器理解這個(gè)注釋下面的方法將作為作業(yè)運(yùn)行。
- 記住,帶@Scheduled 注釋的方法不應(yīng)該有傳遞給它們的參數(shù)。
- 它們也不應(yīng)該返回任何值
- 如果希望在@Scheduled 方法中使用外部對(duì)象,應(yīng)該使用自動(dòng)連接將它們注入到 DemoService 類中,而不是將它們作為參數(shù)傳遞給@Scheduled 方法。
二、固定的延時(shí)和頻率使用@Scheduled
在這個(gè)方法中,fixedDelay 屬性與@Scheduled 注釋一起使用。
舉例:
DemoServiceBasicUsageFixedDelay.java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay { @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) //Or use this public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } 復(fù)制代碼
應(yīng)用程序配置如下:
applicationContext.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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean> </beans>
三、配合cron表達(dá)式使用@Scheduled
在此方法中,cron 屬性與@Scheduled 注釋一起使用。
舉例:
DemoServiceBasicUsageCron.java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageCron { @Scheduled(cron="*/5 * * * * ?") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } }
應(yīng)用程序配置如下:
applicationContext.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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean> </beans>
四、使用properties文件配置Cron
在這個(gè)方法中,cron 屬性與@Scheduled 注釋一起使用。此屬性的值必須是 cron 表達(dá)式,如前面的方法所示,但是,此 cron 表達(dá)式將在屬性文件中定義,相關(guān)屬性的鍵將用于@Scheduled 注釋。
這將使 cron 表達(dá)式與源代碼分離,從而使更改變得容易。
DemoServicePropertiesExample.java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServicePropertiesExample { @Scheduled(cron = "${cron.expression}") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } }
應(yīng)用程序配置如下:
applicationContext.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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <util:properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean> </beans>
五、使用context配置Cron
該方法在屬性文件中配置 cron 表達(dá)式,在配置文件中使用 cron 表達(dá)式的屬性鍵配置作業(yè)調(diào)度。主要的變化是您不需要在任何方法上使用@Scheduled 注釋。方法配置也是在應(yīng)用程序配置文件中完成的。
舉例:
DemoServiceXmlConfig.java package com.howtodoinjava.service; import java.util.Date; public class DemoServiceXmlConfig { public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } }
應(yīng)用程序配置如下:
applicationContext.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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <util:properties id="applicationProps" location="application.properties" /> <context:property-placeholder properties-ref="applicationProps" /> <bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" /> <task:scheduled-tasks> <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled> </task:scheduled-tasks> </beans>
總結(jié)
到此這篇關(guān)于Spring中@Scheduled功能使用的文章就介紹到這了,更多相關(guān)Spring @Scheduled使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring定時(shí)任務(wù)@Scheduled注解(cron表達(dá)式fixedRate?fixedDelay)
- SpringBoot定時(shí)任務(wù)動(dòng)態(tài)擴(kuò)展ScheduledTaskRegistrar詳解
- Spring多線程通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)
- springboot使用定時(shí)器@Scheduled不管用的解決
- springBoot?@Scheduled實(shí)現(xiàn)多個(gè)任務(wù)同時(shí)開始執(zhí)行
- Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例
相關(guān)文章
基于Java并發(fā)容器ConcurrentHashMap#put方法解析
下面小編就為大家?guī)硪黄贘ava并發(fā)容器ConcurrentHashMap#put方法解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06基于JAVA中使用Axis發(fā)布/調(diào)用Webservice的方法詳解
如果初識(shí)axis發(fā)布/調(diào)用WS,建議先讀上面的參考文件,本文對(duì)于發(fā)布/調(diào)用WS的主要步驟只是簡(jiǎn)單文字描述,沒有它寫的詳盡2013-05-05如何理解Java中基類子對(duì)象的構(gòu)建過程從"基類向外"進(jìn)行擴(kuò)散的?
今天小編就為大家分享一篇關(guān)于如何理解Java中基類子對(duì)象的構(gòu)建過程從"基類向外"進(jìn)行擴(kuò)散的?,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04Go Java算法之K個(gè)重復(fù)字符最長(zhǎng)子串詳解
這篇文章主要為大家介紹了Go Java算法之K個(gè)重復(fù)字符最長(zhǎng)子串詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java入門絆腳石之Override和Overload的區(qū)別詳解
重寫是子類對(duì)父類的允許訪問的方法的實(shí)現(xiàn)過程進(jìn)行重新編寫, 返回值和形參都不能改變。即外殼不變,核心重寫!重寫的好處在于子類可以根據(jù)需要,定義特定于自己的行為。重載是在一個(gè)類里面,方法名字相同,而參數(shù)不同。返回類型可以相同也可以不同2021-10-10Java有趣好玩的圖形界面開發(fā)八個(gè)案例實(shí)現(xiàn)
今天使用GUI技術(shù)寫了幾個(gè)練習(xí)的Demo,希望對(duì)大家學(xué)習(xí)圖形用戶界面有所幫助,感興趣的同學(xué)來看看吧,動(dòng)手敲一遍理解更通透2022-05-05Java class文件格式之方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java class文件格式之方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06