栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring 定时任务动态管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring 定时任务动态管理

Spring 定时任务动态管理

pom.xml


  5.6.6
  1.18.20
  2.2.10.RELEASE



  
    org.projectlombok
    lombok
    ${lombok.version}
  
  
    cn.hutool
    hutool-all
    ${hutool.version}
  
  
    org.springframework.boot
    spring-boot-starter-web
    ${spring-boot.web.version}
  

application.yml

server:
  port: 8081

spring:
  application:
    name: SIYUAN-SPRINGBOOT-SERVER

SYSpringBootApplication

package run.siyuan.springboot;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.Set;


@Slf4j
@RestController
@EnableScheduling
@SpringBootApplication
public class SYSpringBootApplication {

    @Autowired
    private ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor;

    public static void main(String[] args) {
        SpringApplication.run(SYSpringBootApplication.class, args);
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void schedulingMessage1() {
        log.info("时间:{} 打印的日志,Method Name:{}", DateUtil.formatDateTime(DateUtil.date()), "schedulingMessage1");
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void schedulingMessage2() {
        log.info("时间:{} 打印的日志,Method Name:{}", DateUtil.formatDateTime(DateUtil.date()), "schedulingMessage2");
    }

    @GetMapping("/cancelTask")
    public Object cancelTask(String methodFullName) {
        Set tasks = scheduledAnnotationBeanPostProcessor.getScheduledTasks();
        for (ScheduledTask task : tasks) {
            if (task.getTask().getRunnable().toString().equals(methodFullName)) {
                task.cancel();
            }
        }
        return "success";
    }

    @GetMapping("/updateTask")
    public String updateTask(@RequestBody JSONObject json) throws NoSuchFieldException, IllegalAccessException {
        String cron = json.getStr("cron");
        String methodFullName = json.getStr("methodFullName");
        Field registrar = scheduledAnnotationBeanPostProcessor.getClass().getDeclaredField("registrar");
        registrar.setAccessible(true);
        ScheduledTaskRegistrar taskRegistrar = (ScheduledTaskRegistrar)registrar.get(scheduledAnnotationBeanPostProcessor);
        TaskScheduler scheduler = taskRegistrar.getScheduler();
        Set scheduledTaskSet = scheduledAnnotationBeanPostProcessor.getScheduledTasks();

        for (ScheduledTask task : scheduledTaskSet){
            if (task.getTask().getRunnable().toString().equals(methodFullName)){
                task.cancel();
                CronTask cronTask = new CronTask(task.getTask().getRunnable(), cron);

                scheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger());
            }
        }
        return "success";
    }

}

验证

### 取消指定定时任务
GET http://localhost:8081/cancelTask?methodFullName=run.siyuan.springboot.SYSpringBootApplication.schedulingMessage1
### 更新任务周期
GET http://localhost:8081/updateTask
Content-Type: application/json

{
  "cron": "0/10 * * * * ?",
  "methodFullName": "run.siyuan.springboot.SYSpringBootApplication.schedulingMessage2"
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号