From f2ff30e4652952cc31302f46249a6fa39c6f1f30 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 25 Nov 2023 23:00:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20error-code=20=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A4=B1=E8=B4=A5=E4=B8=8D?= =?UTF-8?q?=E5=BD=B1=E5=93=8D=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/ErrorCodeAutoGeneratorImpl.java | 8 +++- .../core/loader/ErrorCodeLoader.java | 10 +++++ .../core/loader/ErrorCodeLoaderImpl.java | 39 ++++++++++++------- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java index d670c1829..00fbc03ca 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java @@ -49,8 +49,12 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); // 第二步,写入到 system 服务 - errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); - log.info("[execute][写入到 system 组件完成]"); + try { + errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); + log.info("[execute][写入到 system 组件完成]"); + } catch (Exception ex) { + log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex)); + } } /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java index 26fb060d2..0bf70211f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java @@ -21,4 +21,14 @@ public interface ErrorCodeLoader { ServiceExceptionUtil.put(code, msg); } + /** + * 刷新错误码 + */ + void refreshErrorCodes(); + + /** + * 加载错误码 + */ + void loadErrorCodes(); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java index c1e8b3ef2..4d1febfd7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.framework.errorcode.core.loader; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.exceptions.ExceptionUtil; import cn.iocoder.yudao.framework.common.util.date.DateUtils; import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; @@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import java.time.LocalDateTime; @@ -43,31 +45,38 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader { */ private LocalDateTime maxUpdateTime; + @Override @EventListener(ApplicationReadyEvent.class) + @Async // 异步,保证项目的启动过程,毕竟非关键流程 public void loadErrorCodes() { - this.loadErrorCodes0(); + loadErrorCodes0(); } + @Override @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) public void refreshErrorCodes() { - this.loadErrorCodes0(); + loadErrorCodes0(); } private void loadErrorCodes0() { - // 加载错误码 - List errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); - if (CollUtil.isEmpty(errorCodeRespDTOs)) { - return; - } - log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); + try { + // 加载错误码 + List errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); + if (CollUtil.isEmpty(errorCodeRespDTOs)) { + return; + } + log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); - // 刷新错误码的缓存 - errorCodeRespDTOs.forEach(errorCodeRespDTO -> { - // 写入到错误码的缓存 - putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); - // 记录下更新时间,方便增量更新 - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); - }); + // 刷新错误码的缓存 + errorCodeRespDTOs.forEach(errorCodeRespDTO -> { + // 写入到错误码的缓存 + putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); + // 记录下更新时间,方便增量更新 + maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); + }); + } catch (Exception ex) { + log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex)); + } } }