# Boot-Dynamic-Scheduler **Repository Path**: zhudunfeng/boot-dynamic-scheduler ## Basic Information - **Project Name**: Boot-Dynamic-Scheduler - **Description**: springboot定时任务动态执行器,支持任务自动注册,动态更改定时表达式,动态切换spring-task与xxl-job - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-14 - **Last Updated**: 2025-08-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: Java, SpringBoot, Spring-Task, xxl-job, 定时任务 ## README boot-dynamic-scheduler ====================== 一个基于Spring Boot的动态定时任务调度框架,支持本地定时任务和分布式定时任务(XXL-JOB)。 特性 ---- - **动态配置**:通过配置中心动态修改定时任务参数。 - **本地任务调度**:基于Spring Scheduling实现本地定时任务的管理。 - **分布式任务调度**:集成XXL-JOB,支持分布式环境下定时任务的调度。 - **任务管理**:提供任务的注册、启动、暂停、恢复、修改Cron表达式等操作。 - **REST API**:提供RESTful接口用于任务的管理和监控。 模块结构 -------- - `boot-dynamic-scheduler-core`:核心模块,包含任务调度的基础类和接口。 - `examples`:示例模块,包含本地和分布式任务的使用示例。 快速开始 -------- ### 本地任务示例 1. **添加依赖**:在`pom.xml`中添加本地任务调度的依赖。 ```xml com.adun.scheduler boot-dynamic-scheduler-core ${revision} ``` 2. **启用定时任务**:在Spring Boot应用的主类上添加`@EnableScheduling`注解。 ```java @EnableScheduling @SpringBootApplication public class SpringbootLocalSchedulerApplication { public static void main(String[] args) { SpringApplication.run(SpringbootLocalSchedulerApplication.class, args); } } ``` 3. **定义任务类**:创建一个继承`AbstractSchedulerJob`的任务类,并实现`initJobParams`和`execute`方法。 ```java @Component @RequiredArgsConstructor(onConstructor_ = @__(@Autowired)) @EnableConfigurationProperties(LocalTaskProperties.class) public class SchedulerPrintJob extends AbstractSchedulerJob { private final LocalTaskProperties localTaskProperties; @Override public void initJobParams() { setJobName("schedulerPrintJob"); setJobDescription("打印定时任务"); setJobCron(localTaskProperties.getCron()); setScheduledThreadPoolCoreSize(2); setScheduledThreadPrefix("loc-t-"); } @Override public void execute() { // 任务执行逻辑 } } ``` 4. **配置任务参数**:在`application.yml`中配置任务的Cron表达式和其他参数。 ```yaml local-task: cron: "0/5 * * * * ?" ``` ### 分布式任务示例(XXL-JOB) 1. **添加依赖**:在`pom.xml`中添加XXL-JOB的依赖。 ```xml com.xuxueli xxl-job-core ${xxl-job.version} ``` 2. **启用XXL-JOB**:在Spring Boot应用的主类上添加相关配置。 ```java @EnableScheduling @SpringBootApplication public class SpringbootXxlJobSchedulerApplication { public static void main(String[] args) { SpringApplication.run(SpringbootXxlJobSchedulerApplication.class, args); } } ``` 3. **定义分布式任务类**:创建一个继承`AbstractSchedulerJob`的任务类,并实现`initJobParams`和`execute`方法。 ```java @Component @RequiredArgsConstructor(onConstructor_ = @__(@Autowired)) @EnableConfigurationProperties(XxlTaskProperties.class) public class SchedulerPrintJob extends AbstractSchedulerJob { private final XxlTaskProperties xxlTaskProperties; @Override public void initJobParams() { setJobName("schedulerPrintJob"); setJobDescription("打印定时任务"); setJobCron(xxlTaskProperties.getCron()); setScheduledThreadPoolCoreSize(2); setScheduledThreadPrefix("loc-t-"); } @Override public void execute() { // 任务执行逻辑 } } ``` 4. **配置XXL-JOB参数**:在`application.yml`中配置XXL-JOB的连接信息和任务参数。 ```yaml dynamic: scheduler: xxl-job: enable: true adminAddresses: "http://127.0.0.1:8080/xxl-job-admin" accessToken: "default_token" timeout: 3000 appName: "xxl-job-executor-sample" ip: "127.0.0.1" port: 9999 logPath: "/data/applogs/xxl-job" logRetentionDays: 30 xxl-task: cron: "0/5 * * * * ?" ``` API 接口 --------- - **获取任务列表**:`GET /dynamic/scheduler/job/list` - **获取XXL-JOB执行器初始化SQL**:`GET /dynamic/scheduler/job/xxl-job/executor/init-sql` - **获取XXL-JOB任务初始化SQL**:`GET /dynamic/scheduler/job/xxl-job/task/init-sql` - **暂停任务**:`GET /dynamic/scheduler/job/local/pause?jobName=schedulerPrintJob` - **恢复任务**:`GET /dynamic/scheduler/job/local/resume?jobName=schedulerPrintJob` - **修改任务Cron**:`GET /dynamic/scheduler/job/local/cron?jobName=schedulerPrintJob&cron=0/10 * * * * ?` 贡献 ------ 欢迎贡献代码和提出建议!请参考[贡献指南](CONTRIBUTING.md)了解如何参与项目。 许可证 ------- 该项目使用MIT许可证。详情请查看[LICENSE](LICENSE)文件。