26 changed files with 407 additions and 126 deletions
@ -0,0 +1,34 @@ |
|||
package cc.hiver.mall.mq; |
|||
|
|||
import org.springframework.amqp.core.Binding; |
|||
import org.springframework.amqp.core.BindingBuilder; |
|||
import org.springframework.amqp.core.DirectExchange; |
|||
import org.springframework.amqp.core.Queue; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
/** |
|||
* 优惠券MQ配置 |
|||
*/ |
|||
@Configuration |
|||
public class CouponMqConfig { |
|||
|
|||
public static final String COUPON_EXCHANGE = "mall.coupon.exchange"; |
|||
public static final String COUPON_SEND_QUEUE = "mall.coupon.send.queue"; |
|||
public static final String COUPON_SEND_ROUTING_KEY = "mall.coupon.send.routing.key"; |
|||
|
|||
@Bean |
|||
public DirectExchange couponExchange() { |
|||
return new DirectExchange(COUPON_EXCHANGE, true, false); |
|||
} |
|||
|
|||
@Bean |
|||
public Queue couponSendQueue() { |
|||
return new Queue(COUPON_SEND_QUEUE, true); |
|||
} |
|||
|
|||
@Bean |
|||
public Binding bindingCouponSendQueue() { |
|||
return BindingBuilder.bind(couponSendQueue()).to(couponExchange()).with(COUPON_SEND_ROUTING_KEY); |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
package cc.hiver.mall.mq; |
|||
|
|||
import cc.hiver.mall.controller.MallCouponController.SendCouponReq; |
|||
import cc.hiver.mall.service.mybatis.MallUserCouponService; |
|||
import com.alibaba.fastjson.JSON; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.amqp.rabbit.annotation.RabbitListener; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* 优惠券发放MQ消费者 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
public class CouponSendConsumer { |
|||
|
|||
@Autowired |
|||
private MallUserCouponService mallUserCouponService; |
|||
|
|||
@RabbitListener(queues = CouponMqConfig.COUPON_SEND_QUEUE) |
|||
public void handleCouponSend(String message) { |
|||
log.info("【MQ优惠券消费者】接收到优惠券发放消息: {}", message); |
|||
try { |
|||
SendCouponReq req = JSON.parseObject(message, SendCouponReq.class); |
|||
if (req == null || req.getUserPhones() == null || req.getCouponId() == null) { |
|||
log.warn("【MQ优惠券消费者】消息参数异常, message={}", message); |
|||
return; |
|||
} |
|||
|
|||
mallUserCouponService.send(req.getUserPhones(), req.getType(), req.getCouponId(), req.getGiveNum()); |
|||
log.info("【MQ优惠券消费者】优惠券发放处理成功, 手机号: {}", req.getUserPhones()); |
|||
|
|||
} catch (Exception e) { |
|||
log.error("【MQ优惠券消费者】处理优惠券发放消息异常: {}", e.getMessage(), e); |
|||
throw new org.springframework.amqp.AmqpRejectAndDontRequeueException(e); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue