【功能优化】定时任务的 Bean 不存在时,进行报错提示

This commit is contained in:
YunaiV 2024-09-12 13:44:13 +08:00
parent d8d385e489
commit c71182dda9
2 changed files with 9 additions and 6 deletions

View File

@ -22,7 +22,7 @@ public interface ErrorCodeConstants {
ErrorCode JOB_CHANGE_STATUS_EQUALS = new ErrorCode(1_001_001_003, "定时任务已经处于该状态,无需修改");
ErrorCode JOB_UPDATE_ONLY_NORMAL_STATUS = new ErrorCode(1_001_001_004, "只有开启状态的任务,才可以修改");
ErrorCode JOB_CRON_EXPRESSION_VALID = new ErrorCode(1_001_001_005, "CRON 表达式不正确");
ErrorCode JOB_HANDLER_BEAN_NOT_EXISTS = new ErrorCode(1_001_001_006, "定时任务的处理器 Bean 不存在");
ErrorCode JOB_HANDLER_BEAN_NOT_EXISTS = new ErrorCode(1_001_001_006, "定时任务的处理器 Bean 不存在,注意 Bean 默认首字母小写");
ErrorCode JOB_HANDLER_BEAN_TYPE_ERROR = new ErrorCode(1_001_001_007, "定时任务的处理器 Bean 类型不正确,未实现 JobHandler 接口");
// ========== API 错误日志 1-001-002-000 ==========

View File

@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@ -91,13 +92,15 @@ public class JobServiceImpl implements JobService {
}
private void validateJobHandlerExists(String handlerName) {
Object handler = SpringUtil.getBean(handlerName);
if (handler == null) {
try {
Object handler = SpringUtil.getBean(handlerName);
assert handler != null;
if (!(handler instanceof JobHandler)) {
throw exception(JOB_HANDLER_BEAN_TYPE_ERROR);
}
} catch (NoSuchBeanDefinitionException e) {
throw exception(JOB_HANDLER_BEAN_NOT_EXISTS);
}
if (!(handler instanceof JobHandler)) {
throw exception(JOB_HANDLER_BEAN_TYPE_ERROR);
}
}
@Override