Commit fe2f0ea9 by 张富康

refactor(BusinessReviewService): 优化企微消息推送逻辑为异步执行

- 引入TaskExecutor实现异步任务处理
- 将采购订单企微消息推送改为异步执行
- 将调拨单企微消息推送改为异步执行
- 添加详细日志记录异步任务执行情况
- 提升系统响应性能避免同步阻塞
1 parent a3171618
...@@ -28,6 +28,7 @@ import org.apache.commons.collections.CollectionUtils; ...@@ -28,6 +28,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -65,6 +66,9 @@ public class BusinessReviewService { ...@@ -65,6 +66,9 @@ public class BusinessReviewService {
private StockAllocateDetailService stockAllocateDetailService; private StockAllocateDetailService stockAllocateDetailService;
@Resource @Resource
private StockAllocateReviewService stockAllocateReviewService; private StockAllocateReviewService stockAllocateReviewService;
@Resource
private TaskExecutor taskExecutor;
public Page<PurchaseOrderPageDTO> listWithPage(RpcPagination<PurchaseOrderPageDTO> rpcPagination) { public Page<PurchaseOrderPageDTO> listWithPage(RpcPagination<PurchaseOrderPageDTO> rpcPagination) {
...@@ -133,9 +137,13 @@ public class BusinessReviewService { ...@@ -133,9 +137,13 @@ public class BusinessReviewService {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override @Override
public void afterCommit() { public void afterCommit() {
purchaseOrderService.sendPurchaseOrderWechatMessage(dto.getOrderCode(), ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false); log.info("采购发货-经理审核,推送企微消息,采购订单编号:{}", purchaseOrder.getOrderCode());
taskExecutor.execute(() ->
purchaseOrderService.sendPurchaseOrderWechatMessage(dto.getOrderCode(), ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false));
// 如果存在调拨单号,则推送企微消息给调拨单审核人 // 如果存在调拨单号,则推送企微消息给调拨单审核人
if (CollUtil.isNotEmpty(allocateCodes)) { if (CollUtil.isNotEmpty(allocateCodes)) {
log.info("采购订单转调拨单,推送企微消息给调拨单审核人,调拨单号:{}", JSONUtil.toJsonStr(allocateCodes));
taskExecutor.execute(() -> {
for (String allocateCode : allocateCodes) { for (String allocateCode : allocateCodes) {
log.info("采购订单转调拨单,发送调拨单企微通知,调拨单号:{}", allocateCode); log.info("采购订单转调拨单,发送调拨单企微通知,调拨单号:{}", allocateCode);
// 查询调拨单信息 // 查询调拨单信息
...@@ -144,6 +152,7 @@ public class BusinessReviewService { ...@@ -144,6 +152,7 @@ public class BusinessReviewService {
ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false); ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false);
stockAllocateReviewService.sendStockAllocateWechatMessageToCallOutMerchant(stockAllocateRecord, dto.getUpdatedBy()); stockAllocateReviewService.sendStockAllocateWechatMessageToCallOutMerchant(stockAllocateRecord, dto.getUpdatedBy());
} }
});
} }
} }
}); });
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 异步任务缺乏异常捕获与处理(健壮性问题)

    • 问题描述:在 taskExecutor.execute() 的 Lambda 表达式中,直接调用了第三方接口(企微消息推送)和数据库查询。由于这些逻辑在新的子线程中运行,若发生异常(如网络超时、数据库连接异常),异常会被抛给线程池,导致主流程无法感知,且在默认配置下可能不会被记录到业务错误日志中。
    • 影响分析:如果企微消息发送失败,系统会静默吞掉异常,缺乏失败日志,导致后续难以排查“为什么用户没有收到通知”的问题。
    • 优化建议:在异步任务的代码块内部增加 try-catch 捕获异常,并记录详细的错误日志。 java taskExecutor.execute(() -> { try { purchaseOrderService.sendPurchaseOrderWechatMessage(dto.getOrderCode(), ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false); } catch (Exception e) { log.error("采购发货-经理审核,推送企微消息失败,采购订单编号:{}", purchaseOrder.getOrderCode(), e); } });

    2. 循环内执行数据库单条查询(性能与最佳实践问题)

    • 问题描述:在调拨单的异步处理逻辑中,使用了 for 循环遍历 allocateCodes,并在循环内部调用 stockAllocateRecordMapper.findByAllocateCode(allocateCode) 进行数据库单条查询。
    • 影响分析:典型的 N+1 查询问题。虽然已放入异步线程不阻塞主流程,但当 allocateCodes 数量较多时,会频繁申请数据库连接并产生多次网络 IO,造成不必要的数据库资源浪费。
    • 优化建议:建议在循环外使用 IN 语句进行批量查询,然后在内存中进行遍历处理。 java taskExecutor.execute(() -> { try { // 建议在 Mapper 中新增批量查询方法:findByAllocateCodes(List<String> codes) List<StockAllocateRecord> records = stockAllocateRecordMapper.findByAllocateCodes(allocateCodes); for (StockAllocateRecord record : records) { log.info("采购订单转调拨单,发送调拨单企微通知,调拨单号:{}", record.getAllocateCode()); stockAllocateReviewService.sendStockAllocateWechatMessage(record, ReviewNodeEnum.DELIVERY.getValue(), dto.getUpdatedBy(), false); stockAllocateReviewService.sendStockAllocateWechatMessageToCallOutMerchant(record, dto.getUpdatedBy()); } } catch (Exception e) { log.error("采购订单转调拨单,推送企微消息异常,调拨单号:{}", JSONUtil.toJsonStr(allocateCodes), e); } });

    3. 异步线程可能导致上下文丢失(可维护性与排查问题)

    • 问题描述:使用 TaskExecutor 切换到子线程执行任务时,主线程的 ThreadLocal 变量(如 MDC 中的 TraceId 日志追踪 ID、用户认证上下文等)不会自动传递到子线程。
    • 影响分析:异步任务中打印的日志(如 log.info("采购订单转调拨单..."))将丢失全链路追踪的 TraceId,在 ELK 等日志系统中无法与主请求串联,极大增加线上排查问题的难度。
    • 优化建议:如果系统使用了 MDC 进行链路追踪,建议在提交任务前获取 Context,在异步任务中重新 set;或者推荐配置并使用支持 Context 传递的 TaskExecutor 包装类(如 ThreadPoolTaskExecutor 配合 TaskDecorator)。

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分): 核心业务逻辑正确,利用 TransactionSynchronizationManager.afterCommit() 确保了数据落库后再发送消息,避免了事务未提交就发通知的尴尬情况。扣分项在于异步任务缺乏 try-catch 兜底,健壮性存在瑕疵。未发现违反 MyBatis 子查询和 Dubbo 接口变更的红线规则。

    • 安全性与潜在风险(30/30分): 代码变更主要集中在异步改造,未引入新的 SQL 注入、越权或敏感信息泄露等安全风险。

    • 最佳实践与可维护性(15/20分): 将耗时操作异步化是良好的实践,且引入了详细的日志。扣分项在于循环内查库(N+1问题)以及未考虑异步线程上下文(TraceId)丢失的隐患。

    • 性能与资源利用(4/5分): 通过异步化显著提升了当前接口的响应速度,消除了同步阻塞瓶颈。扣分项在于循环查库对数据库连接资源的轻微浪费。

    • 提交信息质量(5/5分): Commit message 采用了规范的格式(refactor:),清晰地列出了修改点和目的(提升系统响应性能避免同步阻塞),质量很高,便于追溯。

    总分:90分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!