# tasklet-core **Repository Path**: feng-kailong/tasklet-core ## Basic Information - **Project Name**: tasklet-core - **Description**: tasklet定时任务调度平台-core依赖 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2025-12-02 - **Last Updated**: 2025-12-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Tasklet Core Tasklet 是一个分布式定时任务调度平台的 Core 依赖包,提供了任务注册、执行、监控等核心功能。通过简单的注解配置,即可将任务集成到调度平台中进行统一管理和调度。 ## 特性 - 🚀 **简单易用**:通过注解即可快速定义定时任务 - 🔄 **多种任务类型**:支持 Bean 方法任务和 Glue 脚本任务 - 📦 **自动注册**:支持自动向调度中心注册任务 - ⚙️ **灵活配置**:支持自动注册和手动注册两种模式 - 🔔 **任务通知**:支持任务执行结果通知 - ⏱️ **超时控制**:支持任务执行超时监控 - 🔗 **事件总线**:支持 Spring 事件和 RabbitMQ 两种事件发布方式 - 🎯 **生命周期管理**:支持任务初始化、预处理、执行、后处理等完整生命周期 ## 系统要求 - JDK 17+ - Spring Boot 3.2.0+ - Maven 3.6+ ## 快速开始 ### 1. 添加依赖 在项目的 `pom.xml` 中添加依赖: ```xml com.fkl.tasklet tasklet-core 1.0.0 ``` ### 2. 启用 Tasklet 在 Spring Boot 启动类上添加 `@EnableTasklet` 注解: ```java @SpringBootApplication @EnableTasklet public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` ### 3. 配置属性 在 `application.yml` 或 `application.properties` 中添加配置: #### application.yml ```yaml tasklet: executor: # 执行器名称 app-name: my-tasklet-app # 执行器地址 app-address: http://127.0.0.1:8080 # 调度中心地址 admin-addresses: http://127.0.0.1:9090 # 用户名(可选) username: admin # 密码(可选) password: admin123 # 执行器类型:AUTO(自动注册)或 MANUAL(手动注册) executor-type: AUTO # 事件发布类型:COMMON(Spring事件)或 RABBITMQ(RabbitMQ) publisher-type: COMMON # RabbitMQ 配置(当 publisher-type 为 RABBITMQ 时使用) exchange: tasklet-exchange step-queue: tasklet-step-queue step-routing-key: tasklet-step-routing-key notice-queue: tasklet-notice-queue notice-routing-key: tasklet-notice-routing-key ``` #### application.properties ```properties tasklet.executor.app-name=my-tasklet-app tasklet.executor.app-address=http://127.0.0.1:8080 tasklet.executor.admin-addresses=http://127.0.0.1:9090 tasklet.executor.username=admin tasklet.executor.password=admin123 tasklet.executor.executor-type=AUTO tasklet.executor.publisher-type=COMMON ``` ### 4. 创建任务 #### Bean 方法任务 使用 `@TaskletJob` 注解定义定时任务: ```java @Component public class MyTaskService { @TaskletJob( value = "myTask", // 任务名称 cron = "0 */5 * * * ?", // Cron 表达式(每5分钟执行一次) group = "default", // 项目组 project = "myProject" // 项目名称 ) public void executeMyTask(String param) { System.out.println("执行任务: " + param); // 你的任务逻辑 } } ``` #### 使用 TaskletProcessor 定义复杂任务 对于需要完整生命周期管理的任务,可以继承 `TaskletProcessor` 类: ```java @Component @TaskletService public class MyComplexTaskProcessor extends TaskletProcessor { @Override public ResponseResult init(String initContent) { // 初始化逻辑 log.info("任务初始化: {}", initContent); return ResponseResult.success(); } @Override public ResponseResult preProcess(String context, ResponseResult initResult) { // 预处理逻辑 log.info("任务预处理: {}", context); return ResponseResult.success(); } @Override public ResponseResult process(String context, ResponseResult preProcessResult) { // 核心处理逻辑 log.info("任务执行: {}", context); // 你的业务逻辑 return ResponseResult.success(); } @Override public ResponseResult postProcess(String context, ResponseResult processResult) { // 后处理逻辑 log.info("任务后处理: {}", context); return ResponseResult.success(); } @Override public ResponseResult kill(String context) { // 任务终止逻辑(仅在程序运行时被终止时调用) log.info("任务终止: {}", context); return ResponseResult.success(); } } ``` #### 任务超时控制 使用 `@TaskTimeout` 注解设置任务超时时间: ```java @Component public class MyTaskService { @TaskletJob(value = "timeoutTask", cron = "0 0 * * * ?") @TaskTimeout(300) // 超时时间:300秒 public void executeTimeoutTask(String param) { // 任务逻辑 } } ``` #### 任务通知 使用 `@TaskletNotice` 注解标记需要通知的方法: ```java @Component public class MyTaskService { @TaskletJob(value = "noticeTask", cron = "0 0 * * * ?") @TaskletNotice public void executeNoticeTask(String param) { // 任务逻辑 // 执行结果会自动发送通知 } } ``` ## 配置说明 ### 核心配置项 | 配置项 | 说明 | 默认值 | 必填 | |--------|------|--------|------| | `app-name` | 执行器名称 | `tasklet` | 是 | | `app-address` | 执行器地址 | `http://127.0.0.1:8080` | 是 | | `admin-addresses` | 调度中心地址 | `http://127.0.0.1:9090` | 是 | | `username` | 用户名 | - | 否 | | `password` | 密码 | - | 否 | | `executor-type` | 执行器类型:`AUTO`(自动注册)或 `MANUAL`(手动注册) | `AUTO` | 否 | | `publisher-type` | 事件发布类型:`COMMON`(Spring事件)或 `RABBITMQ`(RabbitMQ) | `COMMON` | 否 | ### RabbitMQ 配置(当 publisher-type 为 RABBITMQ 时) | 配置项 | 说明 | 默认值 | |--------|------|--------| | `exchange` | 交换机名称 | `tasklet-exchange` | | `step-queue` | 步骤队列名称 | `tasklet-step-queue` | | `step-routing-key` | 步骤路由键 | `tasklet-step-routing-key` | | `notice-queue` | 通知队列名称 | `tasklet-notice-queue` | | `notice-routing-key` | 通知路由键 | `tasklet-notice-routing-key` | ## API 接口 Tasklet Core 提供了以下 REST API 接口供调度中心调用: ### Bean 任务接口 - `POST /tasklet/bean/auto/trigger` - 自动触发 Bean 任务 - `POST /tasklet/bean/manual/trigger` - 手动触发 Bean 任务 ### Glue 脚本接口 - `POST /tasklet/glue/compile` - 编译 Glue 脚本 - `POST /tasklet/glue/trigger` - 触发 Glue 脚本执行 - `POST /tasklet/glue/check` - 检查 Glue 脚本 ## 任务生命周期 任务执行包含以下生命周期阶段: 1. **START** - 任务开始 2. **INIT** - 初始化阶段(仅 TaskletProcessor) 3. **PRE_PROCESS** - 预处理阶段(仅 TaskletProcessor) 4. **PROCESS** - 核心处理阶段 5. **POST_PROCESS** - 后处理阶段(仅 TaskletProcessor) 6. **FINISH** - 任务完成 7. **KILL** - 任务终止(仅 TaskletProcessor) 8. **CANCEL** - 任务取消 ## 执行器类型 ### AUTO(自动注册) 执行器启动后会自动向调度中心注册任务,并定时发送心跳保持连接。 ```yaml tasklet: executor: executor-type: AUTO ``` ### MANUAL(手动注册) 需要手动向调度中心注册任务,适用于需要精确控制注册时机的场景。 ```yaml tasklet: executor: executor-type: MANUAL ``` ## 事件发布 ### COMMON(Spring 事件) 使用 Spring 的 `ApplicationEventPublisher` 和 `@EventListener` 机制发布和监听事件。 ```yaml tasklet: executor: publisher-type: COMMON ``` ### RABBITMQ 使用 RabbitMQ 发布事件,适用于分布式场景。 ```yaml tasklet: executor: publisher-type: RABBITMQ exchange: tasklet-exchange step-queue: tasklet-step-queue step-routing-key: tasklet-step-routing-key notice-queue: tasklet-notice-queue notice-routing-key: tasklet-notice-routing-key ``` ## 日志配置 建议在 `application.yml` 中配置日志级别: ```yaml logging: level: com.fkl.tasklet: DEBUG ``` ## 验证配置 启动应用后,查看日志中是否有以下信息: - `TaskletAutoConfiguration 开始初始化...` - `配置信息 - appName: xxx, appAddress: xxx, adminAddresses: xxx` - `TaskletAutoConfiguration 初始化完成` ## 常见问题 ### 1. @EnableTasklet 没有效果 - 确保添加了 `spring.factories` 文件(已包含在依赖中) - 检查配置属性是否正确设置 - 查看启动日志是否有相关错误信息 ### 2. 任务没有被注册 - 确保任务方法所在的类有 `@Component` 注解 - 确保任务方法有 `@TaskletJob` 注解 - 检查 `admin-addresses` 配置是否正确 - 检查网络连接是否正常 ### 3. 任务执行失败 - 检查任务方法的参数类型是否正确 - 查看任务执行日志 - 检查是否有异常抛出 ### 4. 超时设置不生效 - 确保方法上添加了 `@TaskTimeout` 注解 - 检查超时时间设置是否合理 ## 依赖说明 Tasklet Core 依赖以下组件: - Spring Boot Web - Spring Boot AOP - FastJSON2 - Lombok - Hutool - OkHttp - Janino(用于 Glue 脚本编译) - RabbitMQ(可选,当使用 RABBITMQ 事件发布时) ## 版本信息 - **当前版本**: 1.0.0 - **Spring Boot 版本**: 3.2.0 - **JDK 版本**: 17+ ## 许可证 [请根据实际情况填写许可证信息] ## 贡献 欢迎提交 Issue 和 Pull Request! ## 联系方式 [请根据实际情况填写联系方式]