热门搜索:和平精英 原神 街篮2 

您的位置:首页 > > 教程攻略 > ai教程 >[059][调度模块]配置驱动的任务仓库 – 从 YAML 加载任务

[059][调度模块]配置驱动的任务仓库 – 从 YAML 加载任务

来源:互联网 更新时间:2026-08-26 21:43

[059][调度模块]配置驱动的任务仓库 – 从 YAML 加载任务

本文章代码: gitee , gitcode , github

[059][调度模块]配置驱动的任务仓库 – 从 YAML 加载任务

摘要

为了让运维更省事、配置也更高效,框架提供了通过 application.yml 声明定时任务的能力。YamlTaskRepository 会把这些配置解析成 YamlTask 对象,同时对外提供完整的增删改查接口。接下来,重点拆解配置结构、加载流程,以及它与 ScheduleTaskManager 之间是如何衔接起来的。

1. 配置结构

tutorials4j:schedule:default-execution:initial-delay: 30s # 默认初始延迟max-failure-count: 3 # 默认最大失败次数tasks:syncOrderTask:class-simple-name: orderSyncRunnercron: "0 0/5 * * * ?"enabled: truedescription: "同步订单"metadata:source: "mysql"execution:max-execution-count: 100due-date: "2025-12-31T23:59:59Z"# 更多任务...
TaskOptions 映射单个任务配置,TaskExecutionOptions 可覆盖全局默认值。 YamlTask.of(TaskOptions) 负责转换为领域对象 YamlTask

2. YamlTaskRepository 初始化

public YamlTaskRepository(ScheduleProperties properties) { properties.getTasks().forEach((name, opts) -> { if (!opts.validate()) throw new IllegalArgumentException(...);YamlTask task = YamlTask.of(opts);task.setName(name);taskMap.put(name, task);});}
构造函数中完成解析,存入 ConcurrentHashMap。 支持后续运行时增删改(createupdatedelete),但默认实现仅做内存操作,可被替换为数据库版本。

3. 与调度器的集成

@Configurationpublic class ScheduleConfiguration { @Bean@ConditionalOnMissingBeanTaskRepository yamlTaskRepository(ScheduleProperties properties) { return new YamlTaskRepository(properties);}@BeanScheduleTaskManager scheduleTaskManager(TaskRepository repository,ObjectProvider consumers) { return new ScheduleTaskManager(repository, consumers.orderedStream().collect(Collectors.toList()));}}
Spring Boot 自动配置将 YamlTaskRepository 作为默认 TaskRepositoryScheduleTaskManager 初始化时会调用 taskRepository.findAll() 加载所有任务并逐一 addTask

4. 运维价值

声明式配置:无需代码修改即可调整任务 cron、开关状态、限流参数。 可替换性:通过 @ConditionalOnMissingBean 允许用户自定义 TaskRepository(如从数据库或配置中心读取)。

热门手游

相关攻略

手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc