From 6dfb8256789299e053b1bcee66c523d1146c5a5d Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Wed, 21 Dec 2022 23:33:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(StockReleaseListener.java):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=95=86=E5=93=81=E5=BA=93=E5=AD=98=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pms/listener/StockReleaseListener.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/listener/StockReleaseListener.java diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/listener/StockReleaseListener.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/listener/StockReleaseListener.java new file mode 100644 index 000000000..ec20d91c1 --- /dev/null +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/listener/StockReleaseListener.java @@ -0,0 +1,49 @@ +package com.youlai.mall.pms.listener; + +import com.rabbitmq.client.Channel; +import com.youlai.mall.pms.service.SkuService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * 商品库存释放监听器 + * + * @author haoxr + * @date 2022/12/20 + */ +@Component +@RequiredArgsConstructor +@Slf4j +public class StockReleaseListener { + + private final SkuService skuService; + + private static final String STOCK_RELEASE_QUEUE = "stock.release.queue"; + private static final String STOCK_EXCHANGE = "stock.exchange"; + private static final String STOCK_RELEASE_ROUTING_KEY = "stock.release.routing.key"; + + @RabbitListener(bindings = + @QueueBinding( + value = @Queue(value = STOCK_RELEASE_QUEUE, durable = "true"), + exchange = @Exchange(value = STOCK_EXCHANGE), + key = {STOCK_RELEASE_ROUTING_KEY} + ), + ackMode = "MANUAL" // 手动ACK + ) + @RabbitHandler + public void handleStockRelease(String orderSn, Message message, Channel channel) throws IOException { + log.info("订单【{}】取消释放库存消息监听", orderSn); + long deliveryTag = message.getMessageProperties().getDeliveryTag(); // 消息序号 + try { + skuService.unlockStock(orderSn); + channel.basicAck(deliveryTag, false); + } catch (Exception e) { + channel.basicReject(deliveryTag, true); + } + } +}