当前位置: 首页 > news >正文

基于Springboot的运行时动态可调的定时任务

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;@Configuration
public class TaskSchedulerConfig {@Bean(destroyMethod = "shutdown")public ThreadPoolTaskScheduler taskScheduler() {ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();scheduler.setPoolSize(自定义池大小,可以通过配置指定大小);scheduler.initialize();return scheduler;}
}

创建和删除定时任务的接口示例

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;@Slf4j
@RestController
@RequestMapping("/task")
public class InnerCommonController {@GetMapping("/c")public ResponseEntity<?> create(@RequestParam("cron") String cron,@RequestParam("taskId") String taskId) {Runnable task = () -> {System.out.println("Executing task: " + taskId + LocalDateTime.now());// 执行任务的具体逻辑};CronTrigger trigger = new CronTrigger(cron);ScheduledFuture<?> future = taskScheduler.schedule(task, trigger);scheduledTasks.put(taskId, future);return ResponseEntity.ok().build();}@GetMapping("/d")public ResponseEntity<?> delete(@RequestParam("taskId") String taskId) {ScheduledFuture<?> future = scheduledTasks.remove(taskId);if (future != null) {future.cancel(false); // 取消任务return ResponseEntity.ok().build();} else {return ResponseEntity.notFound().build();}}
}

调用示例
新增,cron表达式encode了,3秒一次
http://127.0.0.1:8080/demo/task/c?cron=0/3%20*%20*%20*%20*%20?%20&taskId=2024
删除
http://127.0.0.1:8080/demo/task/task/d?taskId=2024

创建后日志打印结果

Executing task: 20242024-08-05T13:50:03.013
Executing task: 20242024-08-05T13:50:06.014
Executing task: 20242024-08-05T13:50:09.003
Executing task: 20242024-08-05T13:50:12.011
Executing task: 20242024-08-05T13:50:15.012
Executing task: 20242024-08-05T13:50:18.007
http://www.lryc.cn/news/416316.html

相关文章:

  • linux perf
  • Linux--网络层IP
  • 浅谈vite之import.meta
  • 【Pytorch实用教程】Pytorch中nn.Sequential的用法
  • Shopify被封?Shopify店铺开店到防封全面指南
  • 11. 盛最多水的容器
  • react如何父子组件传参
  • 【C++】二维数组 数组名
  • 【蘑菇书EasyRL】强化学习,笔记整理
  • 尚硅谷谷粒商城项目笔记——三、安装docker【电脑CPU:AMD】
  • 【8-9月份唯一机械电气计算机主题的IEEE会议】第七届机电一体化与计算机技术工程国际学术会议(MCTE 2024,8月23-25)
  • YOLOv8改进 | 主干网络 | 简单而优雅且有效的VanillaNet 【华为诺亚方舟】
  • Tomcat高可用集群(实例详解)
  • 搭建自己的金融数据源和量化分析平台(五):更新两市退市股票信息
  • Redis复习总结
  • 基于JSP的医院挂号系统
  • Chainlit快速实现AI对话应用1 分钟内实现聊天数据的持久化保存
  • STM32DMA数据传输
  • Python学习笔记50:游戏篇之外星人入侵(十一)
  • vue3踩坑问题记录
  • Python 爬虫实战:Scrapy 框架详解与应用
  • 60 函数参数——关键参数
  • wps 最新 2019 专业版 下载安装教程,解锁全部功能,免费领取
  • 前端(三):Ajax
  • 启动 /使用/关闭 Redis 服务器
  • Linux系统中的高级SELinux安全策略定制技术
  • 使用 Ansible Blocks 进行错误处理
  • java中的静态变量和实例变量的区别
  • 【Effecutive C++】条款02 尽量以const, enum, inline替换 #define
  • leetcode-226. 翻转二叉树