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

SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解

 更新時(shí)間:2022年06月10日 11:30:23   作者:wl_Honest  
這篇文章主要為大家詳細(xì)介紹了SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,需要的可以參考一下

之前寫過文章記錄怎么在SpringBoot項(xiàng)目中簡(jiǎn)單使用定時(shí)任務(wù),不過由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動(dòng)態(tài)修改任務(wù)執(zhí)行時(shí)間,實(shí)在不太靈活。

經(jīng)過網(wǎng)上搜索學(xué)習(xí)后,特此記錄如何在SpringBoot項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)。

因?yàn)橹皇且粋€(gè)demo,所以只引入了需要的依賴:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <optional>true</optional>
        </dependency>
 
        <!-- spring boot 2.3版本后,如果需要使用校驗(yàn),需手動(dòng)導(dǎo)入validation包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

啟動(dòng)類:

package com.wl.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
 
/**
 * @author wl
 * @date 2022/3/22
 */
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.println("(*^▽^*)啟動(dòng)成功!!!(〃'▽'〃)");
    }
}

配置文件application.yml,只定義了服務(wù)端口:

server:
  port: 8089

定時(shí)任務(wù)執(zhí)行時(shí)間配置文件:task-config.ini:

printTime.cron=0/10 * * * * ?

定時(shí)任務(wù)執(zhí)行類:

package com.wl.demo.task;
 
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
import java.util.Date;
 
/**
 * 定時(shí)任務(wù)
 * @author wl
 * @date 2022/3/22
 */
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")
public class ScheduleTask implements SchedulingConfigurer {
 
    @Value("${printTime.cron}")
    private String cron;
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 動(dòng)態(tài)使用cron表達(dá)式設(shè)置循環(huán)間隔
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                log.info("Current time: {}", LocalDateTime.now());
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 使用CronTrigger觸發(fā)器,可動(dòng)態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
            }
        });
    }
}

編寫一個(gè)接口,使得可以通過調(diào)用接口動(dòng)態(tài)修改該定時(shí)任務(wù)的執(zhí)行時(shí)間:

package com.wl.demo.controller;
 
import com.wl.demo.task.ScheduleTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author wl
 * @date 2022/3/22
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
 
    private final ScheduleTask scheduleTask;
 
    @Autowired
    public TestController(ScheduleTask scheduleTask) {
        this.scheduleTask = scheduleTask;
    }
 
    @GetMapping("/updateCron")
    public String updateCron(String cron) {
        log.info("new cron :{}", cron);
        scheduleTask.setCron(cron);
        return "ok";
    }
}

啟動(dòng)項(xiàng)目,可以看到任務(wù)每10秒執(zhí)行一次: 

訪問接口,傳入請(qǐng)求參數(shù)cron表達(dá)式,將定時(shí)任務(wù)修改為15秒執(zhí)行一次:

可以看到任務(wù)變成了15秒執(zhí)行一次

除了上面的借助cron表達(dá)式的方法,還有另一種觸發(fā)器,區(qū)別于CronTrigger觸發(fā)器,該觸發(fā)器可隨意設(shè)置循環(huán)間隔時(shí)間,不像cron表達(dá)式只能定義小于等于間隔59秒。

package com.wl.demo.task;
 
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
import java.util.Date;
 
/**
 * 定時(shí)任務(wù)
 * @author wl
 * @date 2022/3/22
 */
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")
public class ScheduleTask implements SchedulingConfigurer {
 
    @Value("${printTime.cron}")
    private String cron;
 
    private Long timer = 10000L;
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 動(dòng)態(tài)使用cron表達(dá)式設(shè)置循環(huán)間隔
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                log.info("Current time: {}", LocalDateTime.now());
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 使用CronTrigger觸發(fā)器,可動(dòng)態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
//                CronTrigger cronTrigger = new CronTrigger(cron);
//                Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
 
                // 使用不同的觸發(fā)器,為設(shè)置循環(huán)時(shí)間的關(guān)鍵,區(qū)別于CronTrigger觸發(fā)器,該觸發(fā)器可隨意設(shè)置循環(huán)間隔時(shí)間,單位為毫秒
                PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
                Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
            }
        });
    }
}

增加一個(gè)修改時(shí)間的接口:

package com.wl.demo.controller;
 
import com.wl.demo.task.ScheduleTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author wl
 * @date 2022/3/22
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
 
    private final ScheduleTask scheduleTask;
 
    @Autowired
    public TestController(ScheduleTask scheduleTask) {
        this.scheduleTask = scheduleTask;
    }
 
    @GetMapping("/updateCron")
    public String updateCron(String cron) {
        log.info("new cron :{}", cron);
        scheduleTask.setCron(cron);
        return "ok";
    }
 
    @GetMapping("/updateTimer")
    public String updateTimer(Long timer) {
        log.info("new timer :{}", timer);
        scheduleTask.setTimer(timer);
        return "ok";
    }
}

測(cè)試結(jié)果:

到此這篇關(guān)于SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot設(shè)置動(dòng)態(tài)定時(shí)任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java提取兩個(gè)字符串中的相同元素方法

    Java提取兩個(gè)字符串中的相同元素方法

    今天小編就為大家分享一篇Java提取兩個(gè)字符串中的相同元素方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Spring Boot項(xiàng)目添加外部Jar包以及配置多數(shù)據(jù)源的完整步驟

    Spring Boot項(xiàng)目添加外部Jar包以及配置多數(shù)據(jù)源的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot項(xiàng)目添加外部Jar包以及配置多數(shù)據(jù)源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java中接口和抽象類的區(qū)別與相同之處

    Java中接口和抽象類的區(qū)別與相同之處

    這篇文章主要介紹了Java中接口和抽象類的區(qū)別與相同之處,本文講解了抽象類的概念、接口的概念、接口和抽象類的區(qū)別與聯(lián)系等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • Java設(shè)計(jì)模式之Builder建造者模式

    Java設(shè)計(jì)模式之Builder建造者模式

    這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之Builder建造者模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java中Cookie和Session詳解及區(qū)別總結(jié)

    Java中Cookie和Session詳解及區(qū)別總結(jié)

    這篇文章主要介紹了Java中Cookie和Session詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06
  • Java修飾符abstract與static及final的精華總結(jié)

    Java修飾符abstract與static及final的精華總結(jié)

    abstract、static、final三個(gè)修飾符是經(jīng)常會(huì)使用的,對(duì)他們的概念必須非常清楚,弄混了會(huì)產(chǎn)生些完全可以避免的錯(cuò)誤,比如final和abstract不能一同出現(xiàn),static和abstract不能一同出現(xiàn),下面我們來詳細(xì)了解
    2022-04-04
  • Tomcat+Eclipse亂碼問題解決方法與步驟

    Tomcat+Eclipse亂碼問題解決方法與步驟

    亂碼問題是大家在日常開發(fā)過程中經(jīng)常會(huì)遇到的問題,由于各自環(huán)境的不同,解決起來也費(fèi)時(shí)費(fèi)力,本文主要介紹一般性亂碼問題的解決方法與步驟,開發(fā)工具采用Eclipse+Tomcat,統(tǒng)一設(shè)置項(xiàng)目編碼UTF-8為例,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • java同步鎖的正確使用方法(必看篇)

    java同步鎖的正確使用方法(必看篇)

    下面小編就為大家?guī)硪黄猨ava同步鎖的正確使用方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • SpringCloud 如何提取公共配置

    SpringCloud 如何提取公共配置

    這篇文章主要介紹了SpringCloud 提取公共配置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringAOP 構(gòu)造注入的實(shí)現(xiàn)步驟

    SpringAOP 構(gòu)造注入的實(shí)現(xiàn)步驟

    這篇文章主要介紹了SpringAOP_構(gòu)造注入的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-05-05

最新評(píng)論

佛冈县| 西安市| 克山县| 黄大仙区| 惠水县| 长武县| 洪江市| 江华| 繁峙县| 肇庆市| 余姚市| 阿图什市| 贵阳市| 清丰县| 德州市| 周口市| 德化县| 娄烦县| 麟游县| 盐池县| 泗洪县| 年辖:市辖区| 曲水县| 临邑县| 合水县| 光泽县| 冷水江市| 唐山市| 四平市| 许昌县| 灵丘县| 岳阳县| 蓝田县| 聂拉木县| 石家庄市| 库伦旗| 临江市| 承德县| 黄骅市| 高台县| 泸州市|