Commit c71182dda98cdb1993cf9d439cf12ff3f58a485b
1 parent
d8d385e4
【功能优化】定时任务的 Bean 不存在时,进行报错提示
Showing
2 changed files
with
9 additions
and
6 deletions
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ErrorCodeConstants.java
| ... | ... | @@ -22,7 +22,7 @@ public interface ErrorCodeConstants { |
| 22 | 22 | ErrorCode JOB_CHANGE_STATUS_EQUALS = new ErrorCode(1_001_001_003, "定时任务已经处于该状态,无需修改"); |
| 23 | 23 | ErrorCode JOB_UPDATE_ONLY_NORMAL_STATUS = new ErrorCode(1_001_001_004, "只有开启状态的任务,才可以修改"); |
| 24 | 24 | ErrorCode JOB_CRON_EXPRESSION_VALID = new ErrorCode(1_001_001_005, "CRON 表达式不正确"); |
| 25 | - ErrorCode JOB_HANDLER_BEAN_NOT_EXISTS = new ErrorCode(1_001_001_006, "定时任务的处理器 Bean 不存在"); | |
| 25 | + ErrorCode JOB_HANDLER_BEAN_NOT_EXISTS = new ErrorCode(1_001_001_006, "定时任务的处理器 Bean 不存在,注意 Bean 默认首字母小写"); | |
| 26 | 26 | ErrorCode JOB_HANDLER_BEAN_TYPE_ERROR = new ErrorCode(1_001_001_007, "定时任务的处理器 Bean 类型不正确,未实现 JobHandler 接口"); |
| 27 | 27 | |
| 28 | 28 | // ========== API 错误日志 1-001-002-000 ========== | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/job/JobServiceImpl.java
| ... | ... | @@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum; |
| 14 | 14 | import jakarta.annotation.Resource; |
| 15 | 15 | import lombok.extern.slf4j.Slf4j; |
| 16 | 16 | import org.quartz.SchedulerException; |
| 17 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; | |
| 17 | 18 | import org.springframework.stereotype.Service; |
| 18 | 19 | import org.springframework.transaction.annotation.Transactional; |
| 19 | 20 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -91,13 +92,15 @@ public class JobServiceImpl implements JobService { |
| 91 | 92 | } |
| 92 | 93 | |
| 93 | 94 | private void validateJobHandlerExists(String handlerName) { |
| 94 | - Object handler = SpringUtil.getBean(handlerName); | |
| 95 | - if (handler == null) { | |
| 95 | + try { | |
| 96 | + Object handler = SpringUtil.getBean(handlerName); | |
| 97 | + assert handler != null; | |
| 98 | + if (!(handler instanceof JobHandler)) { | |
| 99 | + throw exception(JOB_HANDLER_BEAN_TYPE_ERROR); | |
| 100 | + } | |
| 101 | + } catch (NoSuchBeanDefinitionException e) { | |
| 96 | 102 | throw exception(JOB_HANDLER_BEAN_NOT_EXISTS); |
| 97 | 103 | } |
| 98 | - if (!(handler instanceof JobHandler)) { | |
| 99 | - throw exception(JOB_HANDLER_BEAN_TYPE_ERROR); | |
| 100 | - } | |
| 101 | 104 | } |
| 102 | 105 | |
| 103 | 106 | @Override | ... | ... |