惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - VipSoft

Python 项目简单部署(Linux) MinerU - 将非结构化文档(PDF、图片、Office 文件等)转换为机器可读的 Markdown 和 JSON LangChain 入门 服务端部署-FastAPI LangChain 入门 LangSmith LangChain 入门 实战 - 食谱推荐 LangChain 入门 Memory 会话记忆 LangChain 入门 Tools 工具 LangChain 入门 Tools 工具 LangChain 入门 Prompts 提示词 LangChain 入门 Message 消息 LangChain 入门 Model 的初始化和调用 LangChain 入门 Agent 的基本运行机制 AI 0基础学习,名词解析 LangChain 和 LangGraph AI大模型知识体系 Dify — Workflow - 数据可视化 Dify — 连接MySQL配置 Dify — Chatflow - 数据库智能查询 Dify — Chatflow - 文档知识库 Dify — Agent 智能体 高安全券码、注册码生成 Dify — 文本生成应用 Dify — 聊天助手 -- 知识库 Windows 下 Docker 安装 Dify Ollama — 命令 Ollama — 为什么能够运行不同的模型 Ollama — 接口 Qwen — 自定义模型文件 Ollama Qwen — 安装测试 Ollama Windows 安装 & 指定安装目录 跟着AI学AI - 诊断结论信息抽取 - 批量处理脚本 跟着AI学AI - 诊断结论信息抽取 - 模型压缩与部署 跟着AI学AI - 诊断结论信息抽取 - 模型评估与调试 跟着AI学AI - 诊断结论信息抽取 - 模型训练 轻型民用无人驾驶航空器安全操控理论考试培训材料 FreeRedis Helper Windbg w3wp.DMP 内存分析 跟着AI学AI - 诊断结论信息抽取 - 数据增强 QQ 录屏软件 Java - 加权随机算法 - 示例 Java - 加权随机算法--Demo Java LoadBalanceUtil 负载均衡、轮询加权 SpringBoot 集成 IP2Region 获取IP地域信息 Windows 服务器和虚拟主机,创建.开头的文件夹 .well-known 跟着AI学AI - 诊断结论信息抽取 - 数据格式转换BERT训练格式 跟着AI学AI - 诊断结论信息抽取 - LabelStudio 标注 -- 结论标注 跟着AI学AI - 诊断结论信息抽取 - 学习路径 跟着AI学AI - UV 安装 数据标注工具 Label-Studio `VIRTUAL_ENV=venv` does not match the project environment 跟着AI学AI - 学习路径 - 命名实体识别(NER)和信息抽取(IE) 跟着AI学AI - 需要购买显卡吗? C# 无BOM的UTF-8编码 Vue ref reactive Vue - el-table 嵌套表 Python 找出同步日志中的重复数据 Python - UV PyCharm 不能识别 .venv 的环境
Spring `@Scheduled` 中这些参数的区别、组合和应用场景
VipSoft · 2026-01-08 · via 博客园 - VipSoft

SpringBoot Scheduled 常见用法: https://www.cnblogs.com/vipsoft/p/15751660.html


import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class TestJob {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Scheduled(cron = "*/10 * * * * ?")
    public void crontabTask() throws InterruptedException {
        logger.info("这是基于 cron -- 按系统时钟算 开始: {}", DateUtil.now());
        Thread.sleep(2000);
        logger.info("这是基于 cron -- 按系统时钟算 结束: {}", DateUtil.now());
    }

    @Scheduled(fixedRate = 10 * 1000)
    public void fixedRateTask() throws InterruptedException {
        logger.info("这是基于 fixedRate -- 从上一次启动开始算 开始: {}", DateUtil.now());
        Thread.sleep(2000);
        logger.info("这是基于 fixedRate -- 从上一次启动开始算 结束: {}", DateUtil.now());
    }
 
    @Scheduled(fixedDelay = 10 * 1000)
    public void fixedDelayTask() throws InterruptedException {
        logger.info("这是基于 fixedDelay -- 从上一次结束开始算 开始: {}", DateUtil.now());
        Thread.sleep(2000);
        logger.info("这是基于 fixedDelay -- 从上一次结束开始算 结束: {}", DateUtil.now());
    }
}

image

1. 基本概念对比

cron

  • 定义:使用 Unix/Linux 风格的 cron 表达式
  • 语法秒 分 时 日 月 周 年(可选)
  • 特点
    • 基于日历的调度
    • 执行时间固定
    • 适合在特定时间点执行任务
// 每天凌晨1点执行
@Scheduled(cron = "0 0 1 * * ?")

// 每5分钟执行(在每分钟的0秒执行)
@Scheduled(cron = "0 */5 * * * ?")

// 每小时的10分、30分、50分执行
@Scheduled(cron = "0 10,30,50 * * * ?")

fixedRate

  • 定义:固定频率执行,从上一次开始时间开始计算间隔
  • 特点
    • 固定频率,不关心任务执行时间
    • 如果任务执行时间超过间隔,会立即开始下一次执行
    • 可能造成任务重叠
// 每5分钟执行一次(从上次开始算起)
@Scheduled(fixedRate = 5 * 60 * 1000)

fixedDelay

  • 定义:固定延迟执行,从上一次结束时间开始计算间隔
  • 特点
    • 保证任务执行间隔
    • 不会出现任务重叠
    • 适合需要保证任务串行执行的场景
// 上次执行结束后,等待5分钟再执行下次
@Scheduled(fixedDelay = 5 * 60 * 1000)

initialDelay

  • 定义:首次执行延迟时间
  • 特点
    • 只在第一次执行前等待
    • 可以与 fixedRate 或 fixedDelay 组合使用
    • 不影响后续执行间隔
// 应用启动后等待10分钟,然后每5分钟执行一次
@Scheduled(initialDelay = 10 * 60 * 1000, fixedRate = 5 * 60 * 1000)

2. 执行行为对比

假设任务执行需要2分钟:

// 情况1:fixedRate = 5分钟
// 时间线:0分开始→2分结束→5分开始→7分结束→10分开始...
// 实际间隔:3分钟(5-2)

// 情况2:fixedDelay = 5分钟  
// 时间线:0分开始→2分结束→7分开始→9分结束→14分开始...
// 实际间隔:7分钟(2+5)

// 情况3:cron = "0 */5 * * * ?"
// 时间线:0分开始→2分结束→5分开始→7分结束→10分开始...
// 实际间隔:3分钟,但开始时间固定在0、5、10分

3. 组合使用场景

组合1:initialDelay + fixedRate

@Component
public class DataSyncScheduler {
    
    // 应用启动后等待2小时(让其他服务就绪),然后每30分钟同步一次
    @Scheduled(initialDelay = 2 * 60 * 60 * 1000, 
               fixedRate = 30 * 60 * 1000)
    public void syncData() {
        // 数据同步任务,执行时间较短
    }
}

适用场景

  • 系统启动后需要等待依赖服务就绪
  • 定时数据同步
  • 缓存刷新

组合2:initialDelay + fixedDelay

@Component
public class ReportGenerator {
    
    // 应用启动后等待5分钟,然后每次执行结束后等待1小时再执行
    @Scheduled(initialDelay = 5 * 60 * 1000, 
               fixedDelay = 60 * 60 * 1000)
    public void generateReport() {
        // 生成报表,执行时间较长(约10-20分钟)
        // 保证每次生成完成后,休息1小时再开始
    }
}

适用场景

  • 执行时间较长的任务
  • 需要保证任务不重叠
  • 资源密集型操作

组合3:动态计算 initialDelay

@Component
public class MaintenanceTask {
    
    @PostConstruct
    public void init() {
        // 计算到下一个整点的延迟
    }
    
    @Scheduled(cron = "0 0 */2 * * ?", zone = "Asia/Shanghai")
    public void maintenance() {
        // 每2小时在整点执行系统维护
    }
}

4. 具体应用场景推荐

使用 cron 的场景

  1. 每日定时任务:每天凌晨备份数据库
  2. 工作日特定时间:工作日9:00发送日报
  3. 复杂时间规则:每月最后一天23:30执行
  4. 需要固定执行时刻:整点、半点执行
// 工作日早上9点执行
@Scheduled(cron = "0 0 9 * * MON-FRI")

// 每小时的第5分钟执行
@Scheduled(cron = "0 5 * * * ?")

// 每月1号凌晨2点执行
@Scheduled(cron = "0 0 2 1 * ?")

使用 fixedRate 的场景

  1. 监控类任务:每30秒检查系统状态
  2. 实时数据拉取:每5分钟从API获取最新数据
  3. 心跳检测:每10秒发送心跳包
  4. 缓存刷新:定期刷新缓存,不关心执行时长
// 实时监控,频率优先
@Scheduled(fixedRate = 30 * 1000)  // 每30秒

// 频繁的小任务
@Scheduled(fixedRate = 5 * 1000)   // 每5秒

使用 fixedDelay 的场景

  1. 批处理任务:数据处理完成后需要冷却
  2. 文件处理:处理完一个文件再处理下一个
  3. API调用限制:避免触发API频率限制
  4. 数据库操作:大数据量操作需要间隔
// 处理大量数据,需要间隔
@Scheduled(fixedDelay = 10 * 60 * 1000)  // 每次间隔10分钟

// 调用有限制的第三方API
@Scheduled(fixedDelay = 2 * 1000)  // 每次间隔2秒,避免限流

使用 initialDelay 的场景

  1. 应用启动延迟:等待配置加载完成
  2. 服务发现延迟:等待注册中心就绪
  3. 数据预热:等待缓存加载
  4. 避开启动高峰:应用启动后不立即执行任务
// Spring Cloud 配置,等待配置中心就绪
@Scheduled(initialDelay = 30 * 1000, fixedRate = 60 * 1000)

// 缓存预热后再执行
@Scheduled(initialDelay = 2 * 60 * 1000, cron = "0 */15 * * * ?")

5. 实际项目中的最佳实践

场景:你的需求(8:37启动,8:42执行)

@Component
public class CustodySyncService {
    
    @PostConstruct
    public void init() {
        // 可以在这里计算动态的 initialDelay
    }
    
    // 方案1:使用 cron + 动态初始延迟(需要自己实现)
    // 方案2:使用 fixedRate + 计算的 initialDelay
    
    // 如果坚持要整点开始,建议:
    @Scheduled(cron = "0 */5 * * * ?")
    public void syncCustody() {
        // 这种会固定在 :00, :05, :10 执行
    }
    
    // 如果要从启动时间算,每5分钟:
    @Scheduled(fixedRate = 5 * 60 * 1000)
    public void syncCustody2() {
        // 从启动开始算,每5分钟执行
        // 首次执行是启动后立即执行,除非配合 initialDelay
    }
}

配置建议:

# application.yml
scheduling:
  tasks:
    custody-sync:
      # 从配置读取,方便不同环境调整
      fixed-rate: 300000  # 5分钟
      initial-delay: ${SYNC_INITIAL_DELAY:120000}  # 默认2分钟
      enabled: true

6. 注意事项

  1. 时区问题:cron 表达式默认使用服务器时区,建议显式指定

    @Scheduled(cron = "0 0 9 * * ?", zone = "Asia/Shanghai")
    
  2. 任务重叠问题

    • fixedRate 可能重叠,使用 @Async 或调整线程池
    • fixedDelay 保证不重叠
  3. 异常处理

    • 任务异常不会影响后续调度
    • 建议在方法内部处理异常
  4. 应用集群部署

    • 所有节点都会执行定时任务
    • 需要使用分布式锁或任务调度中间件(如XXL-JOB)

根据你的具体需求,我建议:

  • 如果需要严格的固定时间点 → 用 cron
  • 如果需要固定频率且不关心重叠 → 用 fixedRate
  • 如果需要保证任务串行 → 用 fixedDelay
  • 如果需要延迟启动 → 配合 initialDelay

是否需要加 @Async 取决于你的具体需求:

情况1:不加 @Async(默认单线程)

@Component
@EnableScheduling
public class CronTask3 {
    @Scheduled(fixedDelay = 10000)  // 不加 @Async
    public void first() throws InterruptedException {
        taskLogger.info("任务开始执行,线程: " + Thread.currentThread().getName());
        Thread.sleep(5000);  // 模拟任务执行5秒
        taskLogger.info("任务执行结束");
    }
}

执行效果

10:00:00 任务开始执行,线程: scheduling-1
10:00:05 任务执行结束
10:00:15 任务开始执行,线程: scheduling-1  ← 等待了10秒(5+5)

情况2:加 @Async(多线程异步执行)

@Component
@EnableScheduling
@EnableAsync
public class CronTask3 {
    @Async
    @Scheduled(fixedDelay = 10000)  // 加了 @Async
    public void first() throws InterruptedException {
        taskLogger.info("任务开始执行,线程: " + Thread.currentThread().getName());
        Thread.sleep(5000);  // 模拟任务执行5秒
        taskLogger.info("任务执行结束");
    }
}

执行效果

10:00:00 任务开始执行,线程: task-1
10:00:05 任务执行结束
10:00:10 任务开始执行,线程: task-2  ← 等待了5秒(不是10秒!)

🚨 重要发现:fixedDelay + @Async 的行为变化

当你使用 fixedDelay = 10000 时:

  • 不加 @Async:从上一次方法结束开始算,10秒后执行下一次
  • 加了 @Async:从上一次方法调用开始算,10秒后执行下一次

这是因为 @Async 使方法立即返回(实际在另一个线程执行),Spring 认为方法"结束"了。

🔧 正确组合方案

方案1:真正想要 fixedDelay 效果 + 异步执行

@Component
@EnableScheduling
@EnableAsync
public class CronTask3 {
    
    @Async
    @Scheduled(fixedDelay = 10000)  // 注意:这里的 fixedDelay 行为变了!
    public void first() throws InterruptedException {
        // 实际行为类似 fixedRate,不是真正的 fixedDelay
    }
}

方案2:保持 fixedDelay 特性 + 异步执行(需要手动控制)

@Component
@EnableScheduling
@EnableAsync
public class CronTask3 {
    
    @Async
    @Scheduled(fixedDelay = 10000)
    public CompletableFuture<Void> first() throws InterruptedException {
        return CompletableFuture.runAsync(() -> {
            try {
                taskLogger.info("任务开始执行");
                Thread.sleep(5000);
                taskLogger.info("任务执行结束");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
    }
}

方案3:使用 fixedRate + @Async(更常见)

@Component
@EnableScheduling
@EnableAsync
public class CronTask3 {
    
    @Async
    @Scheduled(fixedRate = 10000)  // 建议使用 fixedRate
    public void first() throws InterruptedException {
        taskLogger.info("任务开始执行");
        Thread.sleep(5000);
        taskLogger.info("任务执行结束");
    }
}

📊 对比表格

组合方式 行为特点 是否重叠 适用场景
fixedDelay 上次结束 → 间隔 → 下次开始 ❌ 不重叠 串行任务,避免并发
fixedRate 上次开始 → 间隔 → 下次开始 ✅ 可能重叠 固定频率,不关心执行时长
fixedDelay + @Async 行为类似 fixedRate ✅ 可能重叠 需要异步但误解了行为
fixedRate + @Async 真正的异步固定频率 ✅ 可能重叠 高并发,短任务,需要异步

🎯 建议

不要加 @Async 的情况

  1. 任务必须串行执行,不能并发
  2. 任务执行时间超过间隔,且需要等待
  3. 任务有状态依赖,不能并行
  4. 使用 fixedDelay 保证间隔

应该加 @Async 的情况

  1. 任务执行时间长,但需要按时调度
  2. 需要并行处理多个定时任务
  3. 任务相互独立,无状态依赖
  4. 使用 fixedRate 保持固定频率

针对你的代码

@Component
@EnableScheduling
public class CronTask3 {
    Logger taskLogger = LoggerFactory.getLogger("crontabTask_3");
    
    // 如果任务执行时间 < 10秒,且需要严格间隔10秒
    // 不要加 @Async
    @Scheduled(fixedDelay = 10000)
    public void first() throws InterruptedException {
        // 你的逻辑
    }
    
    // 如果任务执行时间不确定,需要异步并行
    // 可以加 @Async,但要改用 fixedRate
    @Async
    @Scheduled(fixedRate = 10000)
    public void second() throws InterruptedException {
        // 你的逻辑
    }
}

⚠️ 注意事项

  1. 线程池配置:使用 @Async 需要配置线程池,否则默认线程池可能不够用
  2. 异常处理:异步任务的异常不会传播到调用者
  3. 事务问题@Async 方法的事务边界需要注意
  4. 资源竞争:异步执行可能引起资源竞争,需要同步控制
@Configuration
@EnableAsync
public class AsyncConfig {
    
    @Bean("taskExecutor")
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("async-task-");
        executor.initialize();
        return executor;
    }
}

总结建议

对于你的代码 @Scheduled(fixedDelay = 10000)

  • 如果任务执行很快(< 10秒)→ 不加 @Async
  • 如果任务可能阻塞 → 加 @Async,但考虑改用 fixedRate
  • 如果必须严格间隔10秒 → 不加 @Async,保持 fixedDelay
  • 如果需要并行处理 → 加 @Async,改用 fixedRate