|
|
@ -4,6 +4,7 @@ import cn.nla.common.model.CouponRecordMessage; |
|
|
|
import cn.nla.coupon.service.CouponRecordService; |
|
|
|
import com.rabbitmq.client.Channel; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.redisson.api.RedissonClient; |
|
|
|
import org.springframework.amqp.core.Message; |
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler; |
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener; |
|
|
@ -11,6 +12,7 @@ import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
@ -20,8 +22,8 @@ public class CouponMQListener { |
|
|
|
@Resource |
|
|
|
private CouponRecordService couponRecordService; |
|
|
|
|
|
|
|
// @Resource
|
|
|
|
// private RedissonClient redissonClient;
|
|
|
|
@Resource |
|
|
|
private RedissonClient redissonClient; |
|
|
|
|
|
|
|
/** |
|
|
|
* 重复消费-幂等性 |
|
|
@ -34,8 +36,8 @@ public class CouponMQListener { |
|
|
|
@RabbitHandler |
|
|
|
public void releaseCouponRecord(CouponRecordMessage recordMessage, Message message, Channel channel) throws IOException { |
|
|
|
//防止同个解锁任务并发进入;如果是串行消费不用加锁;加锁有利也有弊,看项目业务逻辑而定
|
|
|
|
//Lock lock = redissonClient.getLock("lock:coupon_record_release:"+recordMessage.getTaskId());
|
|
|
|
//lock.lock();
|
|
|
|
Lock lock = redissonClient.getLock("lock:coupon_record_release:"+recordMessage.getTaskId()); |
|
|
|
lock.lock(); |
|
|
|
log.info("监听到消息:releaseCouponRecord消息内容:{}", recordMessage); |
|
|
|
long msgTag = message.getMessageProperties().getDeliveryTag(); |
|
|
|
boolean flag = couponRecordService.releaseCouponRecord(recordMessage); |
|
|
@ -51,9 +53,9 @@ public class CouponMQListener { |
|
|
|
log.error("释放优惠券记录异常:{},msg:{}", e, recordMessage); |
|
|
|
channel.basicReject(msgTag, true); |
|
|
|
} |
|
|
|
// finally {
|
|
|
|
// lock.unlock();
|
|
|
|
// }
|
|
|
|
finally { |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|