SpringBoot 设置动态定时任务

目录

引入依赖

启动类:

application.yml

 工具类 ScheduleTaskCommon.java

 测试

CronTrigger触发器测试&结果:

PeriodicTrigger触发器测试&结果:


引入依赖


    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-log4j2
        true
    

    
    
        org.springframework.boot
        spring-boot-starter-validation
    

    
        org.projectlombok
        lombok
        true
    

启动类:

@EnableScheduling
@SpringBootApplication
public class HighApplication {

application.yml

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

 工具类 ScheduleTaskCommon.java

package com.springwork.high.common;

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;

/**
 * @author gj
 * @version 1.0.0
 * @date 2023/5/21 21:05
 */
@Component
@Data
@Slf4j
@PropertySource("classpath:/task-config.ini")
public class ScheduleTaskCommon implements SchedulingConfigurer {
    @Value("${printTime.cron}")
    private String cron;

    private Long timer = 10000L;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 使用cron表达式设置循环间隔
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                log.info("CurrentTime: {}", LocalDateTime.now());
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // CronTrigger触发器,修改cron表达式来操作循环规则
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
                // 使用不同的触发器,为设置循环时间的关键,区别于CronTrigger触发器,该触发器可随意设置循环间隔时间,单位为毫秒
                PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
                Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
            }
        });
    }
}

 测试

package com.springwork.high.DateConvert.controller;

import com.springwork.high.common.ScheduleTaskCommon;
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 gj
 * @version 1.0.0
 * @date 2023/5/21 21:37
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class ScheduleTaskController {

    private final ScheduleTaskCommon scheduleTaskCommon;

    @Autowired
    public ScheduleTaskController(ScheduleTaskCommon scheduleTaskCommon) {
        this.scheduleTaskCommon = scheduleTaskCommon;
    }

    @GetMapping("/updateScheduleTaskCron")
    public String updateCron(String cron) {
        log.info("new cron :{}", cron);
        scheduleTaskCommon.setCron(cron);
        return "ok";
    }

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

CronTrigger触发器测试&结果:

SpringBoot 设置动态定时任务_第1张图片

 

 

PeriodicTrigger触发器测试&结果:

SpringBoot 设置动态定时任务_第2张图片

 SpringBoot 设置动态定时任务_第3张图片

 

你可能感兴趣的:(spring,boot,java,spring)