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

Springboot非分布式定時任務(wù)實現(xiàn)代碼

 更新時間:2020年11月19日 09:51:51   作者:大唐冠軍侯  
這篇文章主要介紹了Springboot非分布式定時任務(wù)實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1. 核心注解

在springboot項目中我們可以很方便地使用spring自己的注解@Scheduled和@EnableScheduling配合來實現(xiàn)便捷開發(fā)定時任務(wù)。

@EnableScheduling注解的作用是發(fā)現(xiàn)注解@Scheduled的任務(wù)并后臺執(zhí)行,此注解可以加到啟動類上也可以加到執(zhí)行調(diào)度任務(wù)類上。

經(jīng)測試,當(dāng)有多個包含定時任務(wù)的類時,@EnableScheduling注解加在其中一個類上就可以保證所有定時任務(wù)的成功實現(xiàn)。

注意:定時任務(wù)的類上還需要配合使用@Configuration或@Component注解,這兩個注解都可以。

2. 實例代碼

2.1 @EnableScheduling加在啟動類上;

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Component
public class TestSchedule01 {

  @Scheduled(cron = "0 * * * * ? ")
  public void test() {
    System.out.println("我是定時任務(wù)01,我執(zhí)行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Configuration
public class TestSchedule02 {

  @Scheduled(cron = "1 * * * * ? ")
  public void test() {
    System.out.println("我是定時任務(wù)02,我執(zhí)行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

2.1 @EnableScheduling加在任務(wù)類上;

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Component
@EnableScheduling
public class TestSchedule01 {

  @Scheduled(cron = "0 * * * * ? ")
  public void test() {
    System.out.println("我是定時任務(wù)01,我執(zhí)行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: Karl
 * @date: 2020/10/10
 */
@Configuration
public class TestSchedule02 {

  @Scheduled(cron = "1 * * * * ? ")
  public void test() {
    System.out.println("我是定時任務(wù)02,我執(zhí)行了" + DateUtil.formatDateByDateTime(new Date()));
  }
}

注意:只需要在其中一個任務(wù)類上加上@EnableScheduling注解,所有的定時任務(wù)就都可以正常運行。

3. @Scheduled的幾種用法

@Scheduled這個注解支持3種定時方式,即:cron、fixedRate和fixedDelay

cron:是以表達(dá)式的形式來表示時間,最常見;

fixedRate:表示Scheduled隔多長時間調(diào)用一次,不管任務(wù)是否執(zhí)行完;

fixedDelay:表示該任務(wù)執(zhí)行完后隔多長時間再調(diào)用;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot使用AOP+注解實現(xiàn)簡單的權(quán)限驗證的方法

    SpringBoot使用AOP+注解實現(xiàn)簡單的權(quán)限驗證的方法

    這篇文章主要介紹了SpringBoot使用AOP+注解實現(xiàn)簡單的權(quán)限驗證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 解析Spring RestTemplate必須搭配MultiValueMap的理由

    解析Spring RestTemplate必須搭配MultiValueMap的理由

    本文給大家介紹Spring RestTemplate必須搭配MultiValueMap的理由,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-11-11
  • 簡單介紹Java垃圾回收機(jī)制

    簡單介紹Java垃圾回收機(jī)制

    這篇文章主要介紹了簡單介紹Java垃圾回收機(jī)制,涉及一些相關(guān)的Java術(shù)語,Hotspot虛擬機(jī),jvm體系結(jié)構(gòu)等內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java使用BouncyCastle加密

    Java使用BouncyCastle加密

    本文主要介紹了Java使用BouncyCastle加密,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • springboot連接sqllite遇到的坑及解決

    springboot連接sqllite遇到的坑及解決

    這篇文章主要介紹了springboot連接sqllite遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot接口加密解密統(tǒng)一處理

    SpringBoot接口加密解密統(tǒng)一處理

    這篇文章主要為大家詳細(xì)介紹了SpringBoot接口加密解密統(tǒng)一處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 使用Apache POI在Java中實現(xiàn)Excel單元格的合并

    使用Apache POI在Java中實現(xiàn)Excel單元格的合并

    在日常工作中,Excel是一個不可或缺的工具,尤其是在處理大量數(shù)據(jù)時,本文將介紹如何使用 Apache POI 庫在 Java 中實現(xiàn) Excel 單元格的合并,需要的可以了解下
    2025-03-03
  • Java實現(xiàn)簡單的掃雷小程序

    Java實現(xiàn)簡單的掃雷小程序

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)簡單的掃雷小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 詳解java中String值為空字符串與null的判斷方法

    詳解java中String值為空字符串與null的判斷方法

    這篇文章主要介紹了詳解java中String值為空字符串與null的判斷方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論

卓资县| 资中县| 阳原县| 会东县| 阿坝| 东乌珠穆沁旗| 武宁县| 玛纳斯县| 大埔县| 阳城县| 武清区| 登封市| 长白| 三明市| 咸阳市| 临猗县| 平顶山市| 图木舒克市| 河南省| 乐昌市| 开平市| 庆阳市| 毕节市| 溆浦县| 丘北县| 焦作市| 信丰县| 海兴县| 静宁县| 贡嘎县| 永仁县| 万源市| 安溪县| 石河子市| 孟连| 平舆县| 靖州| 合肥市| 广南县| 晋宁县| 轮台县|