zoukankan      html  css  js  c++  java
  • SpringBoot 动态修改定时任务频率

    原文:https://cloud.tencent.com/developer/article/1627714

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.Trigger;
    import org.springframework.scheduling.TriggerContext;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    import java.util.concurrent.ScheduledFuture;
    
    
    @RestController
    @RequestMapping(value = "test")
    public class TestController {
    
        @Autowired
        private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    
        private ScheduledFuture<?> future;
    
        // 线程池任务调度类
        @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
            return new ThreadPoolTaskScheduler();
        }
    
        private String cronStr = "0/5 * * * * *";
    
        @RequestMapping("/start")
        public String startCron() {
            // 创建定时计划
            future = threadPoolTaskScheduler.schedule(new MyRunnable(), new Trigger(){
                @Override
                public Date nextExecutionTime(TriggerContext triggerContext){
                    return new CronTrigger(cronStr).nextExecutionTime(triggerContext);
                }
            });
            System.out.println("DynamicTask.startCron()");
            return "startCron";
        }
    
        @RequestMapping("/stop")
        public String stopCron() {
    
            if (future != null) {
                future.cancel(true);
            }
            System.out.println("DynamicTask.stopCron()");
            return "stopCron";
        }
    
        @PostMapping("/change")
        public String changeCron(@RequestBody String cron) {
            cronStr = cron;
            // 先停止,在开启
            stopCron();
            future = threadPoolTaskScheduler.schedule(new MyRunnable(), new Trigger(){
                @Override
                public Date nextExecutionTime(TriggerContext triggerContext){
                    return new CronTrigger(cronStr).nextExecutionTime(triggerContext);
                }
            });
            System.out.println("DynamicTask.changeCron()");
            return "changeCron";
        }
    
        private class MyRunnable implements Runnable {
            @Override
            public void run() {
                System.out.println("DynamicTask.MyRunnable.run()," + new Date());
            }
        }
    }

    调试情况如下:

    1.调用start

    2.调用change,并传入0/3 * * * * *

    3.调用stop

    看完打开支付宝扫一扫领个红包吧!


  • 相关阅读:
    那些年,学swift踩过的坑
    JAVA经BigDecimal圆角的解决方案及注意事项
    Jquery简介选择的
    Oracle性能优化顺序表名称来选择最有效的学习笔记
    jQuery Validate插入 reomte使用详细的说明
    公钥私人 ssh避password登陆
    Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
    mysqlbackup 还原特定的表
    OpenJDK 阅读源代码 Java 实现字节流输入类
    Android Application Thread CPU GC Operatiing and OOM Question 0603-随手笔记
  • 原文地址:https://www.cnblogs.com/shihaiming/p/14826007.html
Copyright © 2011-2022 走看看