欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > SpringBoot指定时间执行定时任务

SpringBoot指定时间执行定时任务

2024/10/24 4:30:19 来源:https://blog.csdn.net/weixin_46048207/article/details/140959196  浏览:    关键词:SpringBoot指定时间执行定时任务

任务调度接口:TaskScheduler

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.scheduling;import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import org.springframework.lang.Nullable;public interface TaskScheduler {default Clock getClock() {return Clock.systemDefaultZone();}@NullableScheduledFuture<?> schedule(Runnable task, Trigger trigger);ScheduledFuture<?> schedule(Runnable task, Instant startTime);/** @deprecated */@Deprecated(since = "6.0")default ScheduledFuture<?> schedule(Runnable task, Date startTime) {return this.schedule(task, startTime.toInstant());}ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period);/** @deprecated */@Deprecated(since = "6.0")default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {return this.scheduleAtFixedRate(task, startTime.toInstant(), Duration.ofMillis(period));}ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period);/** @deprecated */@Deprecated(since = "6.0")default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {return this.scheduleAtFixedRate(task, Duration.ofMillis(period));}ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay);/** @deprecated */@Deprecated(since = "6.0")default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {return this.scheduleWithFixedDelay(task, startTime.toInstant(), Duration.ofMillis(delay));}ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay);/** @deprecated */@Deprecated(since = "6.0")default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {return this.scheduleWithFixedDelay(task, Duration.ofMillis(delay));}
}

只执行一次

public class Vo {@NotNull(groups = {AA.class}, message = "规则ID不能为空")@Schema(description = "规则ID")private Long ruleId;@NotNull(groups = {AA.class}, message = "定时不能为空")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")@Schema(description = "定时")private Date time;/*** 质量规则下载-定时下线*/public interface AA{}
}// 定义一个线程安全的集合类,用于存储规则ID和调度任务
private Map<Long, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();public Result offlineRuleTiming(Vo vo) {// 如果taskMap中已包含规则ID,先取消任务,后添加任务if (taskMap.containsKey(vo.getRuleId())) {// 获取调度任务ScheduledFuture<?> scheduledFuture = taskMap.get(vo.getRuleId());if (scheduledFuture != null) {// 取消任务scheduledFuture.cancel(true);}// 移除规则IDtaskMap.remove(vo.getRuleId());}// 添加任务ScheduledFuture<?> schedule = syncScheduler.schedule(() -> {log.info("====质量规则下线定时任务执行开始====");log.info("当前时间:{}", DateUtil.nowStr());log.info("规则ID:{}", vo.getRuleId());// 执行任务逻辑...log.info("====质量规则下线定时任务执行结束====");// 移除规则IDtaskMap.remove(vo.getRuleId());}, vo.getTime().toInstant());// 添入taskMaptaskMap.put(vo.getRuleId(), schedule);// 返回return Result.success();
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com