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

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

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

前言

Spring 為任務調度和基于使用@Scheduled 注釋的 cron 表達式的異步方法執(zhí)行提供了極好的支持??梢詫Scheduled 注釋與觸發(fā)器元數(shù)據(jù)一起添加到方法中。在這篇文章中,我將以4種不同的方式展示@Scheduled 功能的使用方法。

一、Spring @Scheduled Annotation

@ scheduled注釋用于任務調度。觸發(fā)器信息需要與這個注釋一起提供。

您可以使用屬性 fixedDelay/fixedRate/cron 來提供觸發(fā)信息。

  • fixedRate 使 Spring 定期運行任務,即使最后一次調用仍在運行
  • fixedDelay 特別控制最后一次執(zhí)行結束時的下一次執(zhí)行時間。
  • Cron 是一個源自 Unix cron 實用工具的特性,并且根據(jù)您的需求有各種選項。

示例用法如下:

@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 應用程序中使用@Scheduled,必須首先在 applicationConfig.xml 文件中定義 xml 名稱空間和模式位置定義。還添加任務: 注釋驅動,以支持基于注釋的任務調度。

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>

上面的添加是必要的,因為我們將使用基于注釋的配置。

1.3 使用@Scheduled 注釋

下一步是在類中創(chuàng)建一個類和一個方法,如下所示:

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 注釋反過來會使 Spring 容器理解這個注釋下面的方法將作為作業(yè)運行。
  • 記住,帶@Scheduled 注釋的方法不應該有傳遞給它們的參數(shù)。
  • 它們也不應該返回任何值
  • 如果希望在@Scheduled 方法中使用外部對象,應該使用自動連接將它們注入到 DemoService 類中,而不是將它們作為參數(shù)傳遞給@Scheduled 方法。

二、固定的延時和頻率使用@Scheduled

在這個方法中,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());
  }
}
復制代碼

應用程序配置如下:

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表達式使用@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());
  }
}

應用程序配置如下:

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

在這個方法中,cron 屬性與@Scheduled 注釋一起使用。此屬性的值必須是 cron 表達式,如前面的方法所示,但是,此 cron 表達式將在屬性文件中定義,相關屬性的鍵將用于@Scheduled 注釋。

這將使 cron 表達式與源代碼分離,從而使更改變得容易。

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());
  }
}

應用程序配置如下:

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 表達式,在配置文件中使用 cron 表達式的屬性鍵配置作業(yè)調度。主要的變化是您不需要在任何方法上使用@Scheduled 注釋。方法配置也是在應用程序配置文件中完成的。

舉例:

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());
  }
}

應用程序配置如下:

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>

總結

到此這篇關于Spring中@Scheduled功能使用的文章就介紹到這了,更多相關Spring @Scheduled使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

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

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

    java使用smortupload上傳和下載文件

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

    解決Java字符串JSON轉換異常:cn.hutool.json.JSONException:?Mismatched?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論

双峰县| 乳山市| 合江县| 察隅县| 台中县| 睢宁县| 池州市| 永定县| 靖宇县| 定兴县| 景洪市| 封开县| 错那县| 新密市| 隆回县| 龙门县| 绥化市| 通州区| 呈贡县| 富蕴县| 策勒县| 永登县| 磐安县| 屏东市| 大同县| 普格县| 阜新市| 台中市| 鹤庆县| 喀喇沁旗| 青海省| 五家渠市| 铜梁县| 永清县| 安阳县| 泰兴市| 兰州市| 邢台县| 蒙自县| 闽清县| 同心县|