From 31d2c32952c980491f503869dedba27f01e3f856 Mon Sep 17 00:00:00 2001 From: gaowdong Date: Mon, 16 Jun 2025 13:59:56 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E5=AE=9A=E9=A4=90=E4=B8=8B=E5=8D=95?= =?UTF-8?q?=EF=BC=8Cmq=E6=97=A0=E6=B3=95=E8=BF=9E=E6=8E=A5=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/OrderPlaceMobileBusinessImplV3.java | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/order/mobile/business/impl/OrderPlaceMobileBusinessImplV3.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/order/mobile/business/impl/OrderPlaceMobileBusinessImplV3.java index 2d1d774c..e8b5ed9a 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/order/mobile/business/impl/OrderPlaceMobileBusinessImplV3.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/order/mobile/business/impl/OrderPlaceMobileBusinessImplV3.java @@ -5,6 +5,8 @@ import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ObjectUtil; import com.bonus.canteen.core.common.constant.OrderConstant; +import com.bonus.canteen.core.common.redis.RedisUtil; +import com.bonus.canteen.core.common.utils.TenantContextHolder; import com.bonus.canteen.core.order.common.constants.*; import com.bonus.canteen.core.order.common.service.OrderAmountChangeService; import com.bonus.canteen.core.order.common.service.OrderDeliveryService; @@ -68,18 +70,19 @@ import com.bonus.common.houqin.i18n.I18n; import com.bonus.common.houqin.utils.id.Id; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.amqp.rabbit.connection.Connection; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @Service @@ -142,6 +145,8 @@ public class OrderPlaceMobileBusinessImplV3 implements OrderPlaceMobileBusiness @Lazy @Autowired private AllocHolidayApi allocHolidayApi; + @Autowired + private ConnectionFactory connectionFactory; // public OrderPayMobileVO addCurrentMealOrder(RequestHeaderDTO headerDTO, OrderCurrMealTotalDTO currMealTotalDTO) { // LogUtil.info("当餐点餐|入参", currMealTotalDTO); @@ -175,8 +180,51 @@ public class OrderPlaceMobileBusinessImplV3 implements OrderPlaceMobileBusiness // } // } + public boolean isRabbitMqAvailable() { + String mqResultKey = "yst:" + TenantContextHolder.getTenantId() + ":order:reserve:checkMQResult"; + String redisValue = RedisUtil.getString(mqResultKey); + LogUtil.info("MQ检测结果,redis值 : ", redisValue); + if (redisValue != null) { + return "1".equals(redisValue); + } + String lockKey = "yst:" + TenantContextHolder.getTenantId() + ":order:reserve:checkMQ"; + long timeOut = 5 * 60; + Connection connection = null; + try { + if (!RedisUtil.tryLock(lockKey, 10, 15)) { + throw new ServiceException("系统异常,请联系管理员"); + } + redisValue = RedisUtil.getString(mqResultKey); + LogUtil.info("MQ二次检测结果,redis值 : ", redisValue); + if (redisValue != null) { + return "1".equals(redisValue); + } + connection = connectionFactory.createConnection(); + boolean isConnected = connection.isOpen(); + RedisUtil.setString(mqResultKey, isConnected ? "1" : "0", timeOut); + LogUtil.info("MQ连接检测结果 : ", isConnected ? "正常" : "异常"); + return isConnected; + } catch (Exception e) { + LogUtil.info("RabbitMQ 连接检测异常:" + e); + RedisUtil.setString(mqResultKey, "0", timeOut); + return false; + }finally { + RedisUtil.safeUnLock(lockKey); + if(Objects.nonNull(connection)) { + try { + connection.close(); + } catch (Exception ex) { + LogUtil.info("关闭 RabbitMQ 连接异常", ex); + } + } + } + } + public OrderPayMobileVO addReserveMealOrder(RequestHeaderDTO headerDTO, OrderReserveMealTotalDTO reserveMealDTO) { LogUtil.info("预定餐点餐|入参", reserveMealDTO); + if(!isRabbitMqAvailable()) { + throw new ServiceException("系统异常,请联系管理员"); + } JavaxValidateUtils.validate(reserveMealDTO); CustPayVO custInfo = this.getCustInfo(reserveMealDTO.getCustId(), headerDTO.getSourceType()); if (!LeConstants.COMMON_YES.equals(reserveMealDTO.getIfTrial())) {