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

springboot通過注解、接口創(chuàng)建定時任務詳解

 更新時間:2021年07月14日 14:52:35   作者:小草莓子桑  
使用SpringBoot創(chuàng)建定時任務其實是挺簡單的,這篇文章主要給大家介紹了關于springboot如何通過注解、接口創(chuàng)建這兩種方法實現(xiàn)定時任務的相關資料,需要的朋友可以參考下

項目中經(jīng)常會用到定時任務,有的人在用quartz,有的人可能自己搭建了一套調(diào)度平臺,springboot對于定任務的支持,讓定時任務的創(chuàng)建變得簡單,今天來說說springboot中定時任務的創(chuàng)建。

springboot中定時任務的創(chuàng)建

springboot定時任務的創(chuàng)建,這里就主要說兩種方式

  • 通過注解創(chuàng)建
  • 通過springboot中提供的接口實現(xiàn)

springboot通過注解創(chuàng)建定時任務

首先引入pom

在類上主要用到了@EnableScheduling注解,都在org.springframework:spring-context這個包下

就引入org.springframework:spring-context這個包就可以使用了

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
</dependency>

直接上代碼來一個栗子

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @createdTime: 2020/4/7 16:00.
 * @version: 1.0 .
 */
//在類型使用@EnableScheduling來開啟定時任務
@Component
@EnableScheduling
public class TestTask {

    private static ThreadLocal<SimpleDateFormat> dateFormat =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


    //在方法上使用@Scheduled注解來創(chuàng)建具體的定時任務
    @Scheduled(cron = "0/10 * * * * ?")
    private void task1() {
        System.err.println("執(zhí)行定時任務了,執(zhí)行時間為:" + dateFormat.get().format(new Date()));
    }
}

看下執(zhí)行結果:

在類上使用了@EnableScheduling來開啟定時任務,使用了@Component是為了注入到spring容器中,這里不用@Component會不會注入我倒沒有試過,有試過的小伙伴可以說一下。

在具體需要定時執(zhí)行的方法上,使用 @Scheduled注解,這個注解里面的參數(shù)有很多種,我這用了cron表達式,這里介紹下這個注解的參數(shù)吧

@Scheduled注解的各個參數(shù)

  • cron

使用方式:@Scheduled(cron = "0/10 * * * * ?")

源碼定義:String cron() default "";

說明:cron表達式,就是我們?nèi)粘S玫腸ron,具體的就不貼出來了

  • zone

使用方式:@Scheduled(zone = "GMT+08:00")

源碼定義:String zone() default "";

說明:時區(qū),cron表達式會基于這個時區(qū)解析,默認為空,會取應用所在服務器的時區(qū),一般不填就可以了,和jdk中TimeZone用的是統(tǒng)一體系,就不具體說了

  • fixedDelay

使用方式:@Scheduled(fixedDelay = 1)

源碼定義:long fixedDelay() default -1;

說明:上次執(zhí)行完了,相隔多長時間以后再執(zhí)行,單位是毫秒

  • fixedDelayString

使用方式:

@Scheduled(fixedDelayString = "1")

@Scheduled(fixedDelayString = "${配置文件里面的值}")

源碼定義:String fixedDelayString() default "";

說明:和fixedDelay一樣,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣

  • fixedRate

使用方式:@Scheduled(fixedRate = 1)

源碼定義:long fixedRate() default -1;

說明:上次執(zhí)行開始后,相隔多長時間以后再執(zhí)行,單位是毫秒

  • fixedRateString

使用方式:

@Scheduled(fixedRateString = "1")

@Scheduled(fixedRateString = "${配置文件里面的值}")

源碼定義:String fixedRateString() default "";

說明:和fixedRate一樣,,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣

  • initialDelay

使用方式:@Scheduled(initialDelay = 1)

源碼定義:long initialDelay() default -1;

說明:上第一次執(zhí)行后,相隔多長時間以后再執(zhí)行,單位是毫秒

  • initialDelayString

使用方式:

@Scheduled(initialDelayString = "1")

@Scheduled(initialDelayString = "${配置文件里面的值}")

源碼定義:String initialDelayString() default "";

說明:和initialDelay一樣,,是string類型的可以填數(shù),單位是毫秒,可以使用配置文件里面的值,使用方法和spring注入配置文件的使用方式一樣

springboot通過注接口創(chuàng)建定時任務

通過接口創(chuàng)建定時,就會比較靈活,定時cron表達式就不用寫死在代碼的注解上了,可以通過存儲到數(shù)據(jù)庫等存儲系統(tǒng)中,在接口中來獲取這個配置的表達式,這樣可以實現(xiàn)一個簡易的任務調(diào)度平臺,通過數(shù)據(jù)庫配置就可以管理定時任務的執(zhí)行

實現(xiàn)接口SchedulingConfigurer

主要用的是這個接口SchedulingConfigurer,他是org.springframework.scheduling.annotation.SchedulingConfigurer這個包路徑,其實也是都在org.springframework:spring-context這個包下

就引入org.springframework:spring-context這個包就可以使用了

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
</dependency>

主要方法

復寫configureTasks方法,這個方法通過ScheduledTaskRegistrar來添加定時任務,大致看方法,入?yún)⒒臼且粋€線程對象,后面那個參數(shù)和注解里面一樣,主要有cron表達式,delay上次執(zhí)行完了,相隔多長時間以后再執(zhí)行,initial什么的就不一一贅述了

直接上代碼來一個栗子

import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.Task;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @createdTime: 2020/4/7 18:33.
 * @version: 1.0 .
 */
@Component
@EnableScheduling
public class TestTask2 implements SchedulingConfigurer {

    private static ThreadLocal<SimpleDateFormat> dateFormat =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    /**
     * Callback allowing a {@link TaskScheduler
     * TaskScheduler} and specific {@link Task Task}
     * instances to be registered against the given the {@link ScheduledTaskRegistrar}.
     *
     * @param taskRegistrar the registrar to be configured.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        System.err.println("假裝從數(shù)據(jù)庫中獲取到了配置好的定時任務執(zhí)行計劃");
        String cron = "0/10 * * * * ?";
        taskRegistrar.addCronTask(() -> {
            System.err.println("接口定時任務執(zhí)行定時任務了,執(zhí)行時間為:" + dateFormat.get().format(new Date()));
        },cron);
    }
}

這里通過重寫configureTasks方法,使用ScheduledTaskRegistrar對象來創(chuàng)建定時任務,然后表達式可以從數(shù)據(jù)庫等地方讀取,演示時候就不寫那塊代碼了,這樣可以很簡單的實現(xiàn)出來一個簡單的任務調(diào)度平臺
看下執(zhí)行結果:

springboot創(chuàng)建定時任務就為大家說到這里,歡迎大家來交流,指出文中一些說錯的地方,讓我加深認識。

總結

到此這篇關于springboot通過注解、接口創(chuàng)建定時任務的文章就介紹到這了,更多相關springboot創(chuàng)建定時任務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

乌拉特后旗| 洮南市| 抚顺县| 吉林省| 巴彦县| 祁东县| 阜宁县| 西平县| 文水县| 翼城县| 博湖县| 五河县| 万年县| 莆田市| 新乡县| 惠州市| 新兴县| 雷州市| 沐川县| 民权县| 长垣县| 新民市| 奇台县| 广昌县| 安达市| 江孜县| 德安县| 温州市| 长海县| 易门县| 尚志市| 百色市| 西乌| 梅州市| 宁蒗| 淳安县| 安平县| 林州市| 革吉县| 朝阳区| 东明县|