You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring中CronTrigger提前触发线程执行问题咨询

Spring Boot ThreadPoolTaskScheduler 任务提前执行问题

我使用Spring Boot的ThreadPoolTaskScheduler结合CronTrigger实现任务调度,任务仅负责打印当前日期时间,代码如下:

public class MyCron {
    Logger logger = LoggerFactory.getLogger(MyCron);

    private final ThreadPoolTaskScheduler scheduler;
    private final CronTrigger cronTrigger;
    private ScheduledFuture<?> runnableTask;

    public MyCron(String cronEntry) {
        this.scheduler = new ThreadPoolTaskScheduler();
        this.scheduler.setPoolSize(10);
        this.scheduler.setThreadNamePrefix("MyCron-" + name + "-");
        this.scheduler.initialize();
        this.cronTrigger = new CronTrigger(cronEntry);
        runnableTask = scheduler.schedule(this::workerThread, cronTrigger);
    }
}

其中this::workerThread方法用于打印时间,但发现任务总是比指定时间提前执行。例如配置Cron表达式0 */5 * * * *和0 */7 * * * *时,日志显示任务均在分钟的59秒执行:

mycron  | 2024-05-30T17:59:59.844Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 17:59:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:04:59.785Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:04:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:06:59.702Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:06:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:09:59.785Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:09:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:13:59.702Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:13:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:14:59.784Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:14:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:19:59.785Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:19:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:20:59.665Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:20:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:24:59.746Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:24:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:27:59.692Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:27:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:29:59.783Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:29:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:34:59.694Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:34:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:34:59.783Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:34:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:39:59.782Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:39:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:41:59.694Z  INFO 1 --- [n-Cron test 2-7] c.e   : Cron test 2 worker is running: Thu May 30 2024 18:41:59 GMT+0000 (UTC)
mycron  | 2024-05-30T18:44:59.790Z  INFO 1 --- [n-Cron test 1-1] c.e   : Cron test 1 worker is running: Thu May 30 2024 18:44:59 GMT+0000 (UTC)

我配置的CronTrigger秒数为0,预期任务在整分的0秒执行,比如0 */5 * * * *应在18:05:00执行,实际却在18:04:59执行。当前使用的版本为Spring Boot v3.3.0、Spring v6.1.8。

该问题已影响业务逻辑:任务中执行的SQL查询包含where timestamp < current_timestamp()条件,因任务提前执行且运行速度快,导致条件未触发。

请问这是什么原因导致的?是否存在配置错误?


内容的提问来源于stack exchange,提问作者BUKTOP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:08:11