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

Spring中@Scheduled用cron时不自动订阅返回的Mono问题

Spring @Scheduled 对返回Mono方法的订阅行为差异

以下是两种不同@Scheduled配置下的具体行为:

  • Cron表达式配置场景
    当使用Cron表达式定义定时任务时:

    @Scheduled(cron = "0 0/5 16-19 * * *")
    

    Spring不会自动订阅方法返回的Mono,你需要手动调用.subscribe()来触发异步任务执行。

  • 固定延迟+初始延迟配置场景
    当使用初始延迟和固定延迟参数定义定时任务时:

    @Scheduled(initialDelay = 2, fixedDelay = 5, timeUnit = TimeUnit.MINUTES)
    

    Spring会自动处理方法返回的Mono,自动完成订阅操作,无需手动干预。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 07:45:54