任务调度接口: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();
}
@Nullable
ScheduledFuture<?> 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);
}
// 移除规则ID
taskMap.remove(vo.getRuleId());
}
// 添加任务
ScheduledFuture<?> schedule = syncScheduler.schedule(() -> {
log.info("====质量规则下线定时任务执行开始====");
log.info("当前时间:{}", DateUtil.nowStr());
log.info("规则ID:{}", vo.getRuleId());
// 执行任务逻辑
...
log.info("====质量规则下线定时任务执行结束====");
// 移除规则ID
taskMap.remove(vo.getRuleId());
}, vo.getTime().toInstant());
// 添入taskMap
taskMap.put(vo.getRuleId(), schedule);
// 返回
return Result.success();
}