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

SpringBoot實(shí)現(xiàn)設(shè)置動態(tài)定時任務(wù)的方法詳解

 更新時間:2023年10月18日 10:22:33   作者:gblfy  
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)設(shè)置動態(tài)定時任務(wù)的方法詳解,SpringBoot是一個快速開發(fā)的Java框架,而動態(tài)定時任務(wù)是指可以在運(yùn)行時動態(tài)添加、修改和刪除定時任務(wù)的功能,需要的朋友可以參考下

一、第一種

1. 引入依賴

<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ǎ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>

2. 添加注解

@EnableScheduling
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.println("(*^▽^*)啟動成功!!!(〃'▽'〃)");
    }
}

3. application.yml

只定義了服務(wù)端口:

server: port: 8089

4. task-config.ini

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

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

5. 定時任務(wù)執(zhí)行類

package com.gblfy.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;
 
/**
 * 定時任務(wù)
 * @author gblfy
 */
@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) {
        // 動態(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ā)器,可動態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
            }
        });
    }
}

6. 編寫一個接口

使得可以通過調(diào)用接口動態(tài)修改該定時任務(wù)的執(zhí)行時間:

package com.face.controller;

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 gblfy
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    private final ScheduleTask scheduleTask;

    @Autowired
    public TestController(ScheduleTask scheduleTask) {
        this.scheduleTask = scheduleTask;
    }
    //http://localhost:8089/test/updateCron?cron=0/5%20*%20*%20*%20*%20?
    @GetMapping("/updateCron")
    public String updateCron(String cron) {
        log.info("new cron :{}", cron);
        scheduleTask.setCron(cron);
        return "ok";
    }
    //http://localhost:8089/test/updateTimer?timer=5000
    @GetMapping("/updateTimer")
    public String updateTimer(Long timer) {
        log.info("new timer :{}", timer);
        scheduleTask.setTimer(timer);
        return "ok";
    }
}

啟動項(xiàng)目,可以看到任務(wù)每10秒執(zhí)行一次: 訪問接口,傳入請求參數(shù)cron表達(dá)式,將定時任務(wù)修改為15秒執(zhí)行一次: 可以看到任務(wù)變成了15秒執(zhí)行一次

http://localhost:8089/test/updateCron?cron=0/5 * * * * ?

二、第二種

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

2.1. 調(diào)整定時任務(wù)類

package com.face.controller;

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;

/**
 * 定時任務(wù)
 * @author wl
 */
@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) {
        // 動態(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ā)器,可動態(tài)修改cron表達(dá)式來操作循環(huán)規(guī)則
//                CronTrigger cronTrigger = new CronTrigger(cron);
//                Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);

                // 使用不同的觸發(fā)器,為設(shè)置循環(huán)時間的關(guān)鍵,區(qū)別于CronTrigger觸發(fā)器,該觸發(fā)器可隨意設(shè)置循環(huán)間隔時間,單位為毫秒
                PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
                Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;`在這里插入代碼片`
            }
        });
    }
}

2.2. 添加更新方法

  @GetMapping("/updateTimer")
    public String updateTimer(Long timer) {
        log.info("new timer :{}", timer);
        scheduleTask.setTimer(timer);
        return "ok";
    }

2.3. 測試

//localhost:8089/test/updateTimer?timer=5000

三、推薦方案

上面有很多弊端,弊端定時任務(wù)監(jiān)控,單機(jī)故障等等,建議采用分布式任務(wù)調(diào)度框架xxl-job

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

相關(guān)文章

最新評論

丽江市| 读书| 房产| 黄骅市| 沈阳市| 荔波县| 永春县| 旬邑县| 靖江市| 大埔区| 永兴县| 滨州市| 巴彦县| 新田县| 绥阳县| 新河县| 荆门市| 金寨县| 儋州市| 新巴尔虎右旗| 定安县| 和平区| 辽宁省| 抚州市| 道孚县| 通渭县| 罗田县| 慈利县| 海兴县| 山西省| 金山区| 绩溪县| 哈尔滨市| 尉氏县| 偃师市| 焦作市| 石门县| 武城县| 六盘水市| 株洲市| 永昌县|