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();
    }

    @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();
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值