Commit f0f5b5215045e9e34ed51ce102d2bf2d174710ae
1 parent
08d39d45
默认禁用 quartz 依赖,保证启动速度
Showing
3 changed files
with
28 additions
and
4 deletions
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.framework.quartz.config; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager; |
| 4 | +import lombok.extern.slf4j.Slf4j; | |
| 4 | 5 | import org.quartz.Scheduler; |
| 5 | 6 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 6 | 7 | import org.springframework.context.annotation.Bean; |
| 7 | 8 | import org.springframework.scheduling.annotation.EnableScheduling; |
| 8 | 9 | |
| 10 | +import java.util.Optional; | |
| 11 | + | |
| 9 | 12 | /** |
| 10 | 13 | * 定时任务 Configuration |
| 11 | 14 | */ |
| 12 | 15 | @AutoConfiguration |
| 13 | 16 | @EnableScheduling // 开启 Spring 自带的定时任务 |
| 17 | +@Slf4j | |
| 14 | 18 | public class YudaoQuartzAutoConfiguration { |
| 15 | 19 | |
| 16 | 20 | @Bean |
| 17 | - public SchedulerManager schedulerManager(Scheduler scheduler) { | |
| 18 | - return new SchedulerManager(scheduler); | |
| 21 | + public SchedulerManager schedulerManager(Optional<Scheduler> scheduler) { | |
| 22 | + if (!scheduler.isPresent()) { | |
| 23 | + log.info("[定时任务 - 已禁用][参考 https://doc.iocoder.cn/job/ 开启]"); | |
| 24 | + return new SchedulerManager(null); | |
| 25 | + } | |
| 26 | + return new SchedulerManager(scheduler.get()); | |
| 19 | 27 | } |
| 20 | 28 | |
| 21 | 29 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/core/scheduler/SchedulerManager.java
| ... | ... | @@ -4,6 +4,9 @@ import cn.iocoder.yudao.framework.quartz.core.enums.JobDataKeyEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.quartz.core.handler.JobHandlerInvoker; |
| 5 | 5 | import org.quartz.*; |
| 6 | 6 | |
| 7 | +import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED; | |
| 8 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; | |
| 9 | + | |
| 7 | 10 | /** |
| 8 | 11 | * {@link org.quartz.Scheduler} 的管理器,负责创建任务 |
| 9 | 12 | * |
| ... | ... | @@ -37,6 +40,7 @@ public class SchedulerManager { |
| 37 | 40 | public void addJob(Long jobId, String jobHandlerName, String jobHandlerParam, String cronExpression, |
| 38 | 41 | Integer retryCount, Integer retryInterval) |
| 39 | 42 | throws SchedulerException { |
| 43 | + validateScheduler(); | |
| 40 | 44 | // 创建 JobDetail 对象 |
| 41 | 45 | JobDetail jobDetail = JobBuilder.newJob(JobHandlerInvoker.class) |
| 42 | 46 | .usingJobData(JobDataKeyEnum.JOB_ID.name(), jobId) |
| ... | ... | @@ -61,6 +65,7 @@ public class SchedulerManager { |
| 61 | 65 | public void updateJob(String jobHandlerName, String jobHandlerParam, String cronExpression, |
| 62 | 66 | Integer retryCount, Integer retryInterval) |
| 63 | 67 | throws SchedulerException { |
| 68 | + validateScheduler(); | |
| 64 | 69 | // 创建新 Trigger 对象 |
| 65 | 70 | Trigger newTrigger = this.buildTrigger(jobHandlerName, jobHandlerParam, cronExpression, retryCount, retryInterval); |
| 66 | 71 | // 修改调度 |
| ... | ... | @@ -74,6 +79,7 @@ public class SchedulerManager { |
| 74 | 79 | * @throws SchedulerException 删除异常 |
| 75 | 80 | */ |
| 76 | 81 | public void deleteJob(String jobHandlerName) throws SchedulerException { |
| 82 | + validateScheduler(); | |
| 77 | 83 | scheduler.deleteJob(new JobKey(jobHandlerName)); |
| 78 | 84 | } |
| 79 | 85 | |
| ... | ... | @@ -84,6 +90,7 @@ public class SchedulerManager { |
| 84 | 90 | * @throws SchedulerException 暂停异常 |
| 85 | 91 | */ |
| 86 | 92 | public void pauseJob(String jobHandlerName) throws SchedulerException { |
| 93 | + validateScheduler(); | |
| 87 | 94 | scheduler.pauseJob(new JobKey(jobHandlerName)); |
| 88 | 95 | } |
| 89 | 96 | |
| ... | ... | @@ -94,6 +101,7 @@ public class SchedulerManager { |
| 94 | 101 | * @throws SchedulerException 启动异常 |
| 95 | 102 | */ |
| 96 | 103 | public void resumeJob(String jobHandlerName) throws SchedulerException { |
| 104 | + validateScheduler(); | |
| 97 | 105 | scheduler.resumeJob(new JobKey(jobHandlerName)); |
| 98 | 106 | scheduler.resumeTrigger(new TriggerKey(jobHandlerName)); |
| 99 | 107 | } |
| ... | ... | @@ -108,11 +116,12 @@ public class SchedulerManager { |
| 108 | 116 | */ |
| 109 | 117 | public void triggerJob(Long jobId, String jobHandlerName, String jobHandlerParam) |
| 110 | 118 | throws SchedulerException { |
| 119 | + validateScheduler(); | |
| 120 | + // 触发任务 | |
| 111 | 121 | JobDataMap data = new JobDataMap(); // 无需重试,所以不设置 retryCount 和 retryInterval |
| 112 | 122 | data.put(JobDataKeyEnum.JOB_ID.name(), jobId); |
| 113 | 123 | data.put(JobDataKeyEnum.JOB_HANDLER_NAME.name(), jobHandlerName); |
| 114 | 124 | data.put(JobDataKeyEnum.JOB_HANDLER_PARAM.name(), jobHandlerParam); |
| 115 | - // 触发任务 | |
| 116 | 125 | scheduler.triggerJob(new JobKey(jobHandlerName), data); |
| 117 | 126 | } |
| 118 | 127 | |
| ... | ... | @@ -127,4 +136,11 @@ public class SchedulerManager { |
| 127 | 136 | .build(); |
| 128 | 137 | } |
| 129 | 138 | |
| 139 | + private void validateScheduler() { | |
| 140 | + if (scheduler == null) { | |
| 141 | + throw exception0(NOT_IMPLEMENTED.getCode(), | |
| 142 | + "[定时任务 - 已禁用][参考 https://doc.iocoder.cn/job/ 开启]"); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 130 | 146 | } | ... | ... |
yudao-server/src/main/resources/application-local.yaml
| ... | ... | @@ -9,6 +9,7 @@ spring: |
| 9 | 9 | exclude: |
| 10 | 10 | - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源 |
| 11 | 11 | - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration # 排除积木报表带来的 MongoDB 的自动配置 |
| 12 | + - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 默认 local 环境,不开启 Quartz 的自动配置 | |
| 12 | 13 | datasource: |
| 13 | 14 | druid: # Druid 【监控】相关的全局配置 |
| 14 | 15 | web-stat-filter: |
| ... | ... | @@ -78,7 +79,6 @@ spring: |
| 78 | 79 | # Quartz 配置项,对应 QuartzProperties 配置类 |
| 79 | 80 | spring: |
| 80 | 81 | quartz: |
| 81 | - auto-startup: false # 本地开发环境,尽量不要开启 Job | |
| 82 | 82 | scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName |
| 83 | 83 | job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。 |
| 84 | 84 | wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true | ... | ... |