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

配置类

java 复制代码
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;
    }
}

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

java 复制代码
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 \* \* \* \* ? \&taskId=2024

删除

http://127.0.0.1:8080/demo/task/task/d?taskId=2024

创建后日志打印结果

java 复制代码
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
相关推荐
野生的码农5 小时前
码农的妇产科实习记录
android·java·人工智能
盖世英雄酱581365 小时前
Java 组长年终总结:靠 AI 提效 50%,25 年搞副业只赚 4k?
后端·程序员·trae
毕设源码-赖学姐6 小时前
【开题答辩全过程】以 高校人才培养方案管理系统的设计与实现为例,包含答辩的问题和答案
java
+VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue在线音乐播放系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
一起努力啊~6 小时前
算法刷题-二分查找
java·数据结构·算法
小途软件6 小时前
高校宿舍访客预约管理平台开发
java·人工智能·pytorch·python·深度学习·语言模型
J_liaty6 小时前
Java版本演进:从JDK 8到JDK 21的特性革命与对比分析
java·开发语言·jdk
code bean6 小时前
Flask图片服务在不同网络接口下的路径解析问题及解决方案
后端·python·flask
+VX:Fegn08957 小时前
计算机毕业设计|基于springboot + vue律师咨询系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
努力的小郑7 小时前
2025年度总结:当我在 Cursor 里敲下 Tab 的那一刻,我知道时代变了
前端·后端·ai编程