最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中@Scheduled功能的使用方法詳解

 更新時(shí)間:2022年04月08日 12:24:49   作者:青Cheng序員石頭  
@Scheduled 由Spring定義,用于將方法設(shè)置為調(diào)度任務(wù),下面這篇文章主要給大家介紹了關(guān)于Spring中@Scheduled功能的使用方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

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
{
 &nbsp;@Scheduled(fixedDelay = 5000)
 &nbsp;//@Scheduled(fixedRate = 5000)  //Or use this
 &nbsp;public void demoServiceMethod()
  {
 &nbsp; &nbsp;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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Javaweb中使用Servlet編寫簡單的接口案例詳解

    Javaweb中使用Servlet編寫簡單的接口案例詳解

    文章介紹了如何使用Servlet編寫一個(gè)簡單的接口來校驗(yàn)用戶提交的密碼長度是否在6到12位之間,代碼分為后端部分和前端部分,給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-02-02
  • java使用smortupload上傳和下載文件

    java使用smortupload上傳和下載文件

    這篇文章主要介紹了java使用smortupload上傳和下載文件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 解決Java字符串JSON轉(zhuǎn)換異常:cn.hutool.json.JSONException:?Mismatched?hr?and?body

    解決Java字符串JSON轉(zhuǎn)換異常:cn.hutool.json.JSONException:?Mismatched?

    這篇文章主要給大家介紹了關(guān)于如何解決Java字符串JSON轉(zhuǎn)換異常:cn.hutool.json.JSONException:?Mismatched?hr?and?body的相關(guān)資料,文中將解決的辦法通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • 使用原生JDBC動(dòng)態(tài)解析并獲取表格列名和數(shù)據(jù)的方法

    使用原生JDBC動(dòng)態(tài)解析并獲取表格列名和數(shù)據(jù)的方法

    這篇文章主要介紹了使用原生JDBC動(dòng)態(tài)解析并獲取表格列名和數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 利用Spring AOP記錄方法的執(zhí)行時(shí)間

    利用Spring AOP記錄方法的執(zhí)行時(shí)間

    這篇文章給大家介紹的是spring的aop來實(shí)現(xiàn)方法級(jí)的執(zhí)行時(shí)間的記錄監(jiān)控,以此來評(píng)估方法的性能以及針對(duì)性的對(duì)已存在的方法進(jìn)行優(yōu)化。對(duì)于監(jiān)控,我們比較關(guān)注監(jiān)控的可靠性和性能,準(zhǔn)確,高效,這才能在不影響整體性能的情況下對(duì)我們的系統(tǒng)性能有個(gè)較準(zhǔn)確的認(rèn)識(shí)。
    2016-09-09
  • MyBatis 多個(gè)條件使用Map傳遞參數(shù)進(jìn)行批量刪除方式

    MyBatis 多個(gè)條件使用Map傳遞參數(shù)進(jìn)行批量刪除方式

    這篇文章主要介紹了MyBatis 多個(gè)條件使用Map傳遞參數(shù)進(jìn)行批量刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot集成FastDFS實(shí)現(xiàn)防盜鏈功能

    SpringBoot集成FastDFS實(shí)現(xiàn)防盜鏈功能

    FastDFS是一個(gè)高性能的分布式?件系統(tǒng),本文將為大家詳細(xì)介紹一下SpringBoot如何集成FastDFS實(shí)現(xiàn)防盜鏈功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • 解讀RedisTemplate的各種操作(set、hash、list、string)

    解讀RedisTemplate的各種操作(set、hash、list、string)

    這篇文章主要介紹了解讀RedisTemplate的各種操作(set、hash、list、string),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • eclipse上配置Maven的圖文教程(推薦)

    eclipse上配置Maven的圖文教程(推薦)

    下面小編就為大家分享一篇eclipse上配置Maven的圖文教程(推薦),具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能

    SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能

    本文主要介紹了Spring Boot 集成ElasticSearch實(shí)現(xiàn)搜索功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03

最新評(píng)論

瓮安县| 镇巴县| 象州县| 乐平市| 普宁市| 璧山县| 师宗县| 修文县| 许昌县| 砀山县| 呼和浩特市| 三原县| 荥阳市| 兴安盟| 个旧市| 绍兴市| 彭水| 阳谷县| 襄汾县| 禹城市| 祥云县| 莱芜市| 淮安市| 阳新县| 贵定县| 昌邑市| 云阳县| 德清县| 阿城市| 平潭县| 乌兰察布市| 石林| 井陉县| 离岛区| 夏河县| 宣汉县| 江口县| 武强县| 资阳市| 阿瓦提县| 远安县|