|
|
@ -3,9 +3,11 @@ package com.luoo.user.listener;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
import com.luoo.user.service.UserPointLogService;
|
|
|
|
import com.luoo.user.service.UserPointLogService;
|
|
|
|
|
|
|
|
import com.luoo.user.util.DistributedLock;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
@ -18,13 +20,19 @@ import java.io.IOException;
|
|
|
|
* @create: 2024-07-24 13:05
|
|
|
|
* @create: 2024-07-24 13:05
|
|
|
|
**/
|
|
|
|
**/
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
@RabbitListener(queues = "pointLog", containerFactory = "pointLogFactory")
|
|
|
|
@RabbitListener(queues = "pointLog")
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class PointLogListener {
|
|
|
|
public class PointLogListener {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private UserPointLogService userPointLogService;
|
|
|
|
private UserPointLogService userPointLogService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private DistributedLock distributedLock;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private RabbitTemplate rabbitTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
@RabbitHandler
|
|
|
|
@RabbitHandler
|
|
|
|
public void executePointLog(String json) {
|
|
|
|
public void executePointLog(String json) {
|
|
|
|
log.info("userPointLog:{}", json);
|
|
|
|
log.info("userPointLog:{}", json);
|
|
|
@ -33,7 +41,24 @@ public class PointLogListener {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
UserPointLog userPointLog = objectMapper.readValue(json, UserPointLog.class);
|
|
|
|
UserPointLog userPointLog = objectMapper.readValue(json, UserPointLog.class);
|
|
|
|
|
|
|
|
String userId = userPointLog.getUserId();
|
|
|
|
|
|
|
|
// 对userId进行redis分布式锁
|
|
|
|
|
|
|
|
// springboot 写一段setnx的分布式锁
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String key = "pointLog:" + userId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (distributedLock.tryLock(key, "1")) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 执行需要加锁的业务逻辑
|
|
|
|
userPointLogService.add(userPointLog);
|
|
|
|
userPointLogService.add(userPointLog);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
// 释放锁
|
|
|
|
|
|
|
|
distributedLock.unlock(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 获取锁失败,处理相应逻辑
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend("pointLog", json);
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|