用户监听日志记录

将用户的一系列行为记录下来,为后续的后台管理,以及用户评分(大数据推送依据)提供数据支撑

本数据记录到log表中

1.用户行为

    /**
     * 操作类型,
     * 0101为登录,0102为注册,
     * 0201为发动态,0202为浏览动态,0203为动态点赞,0204为动态喜欢,0205为评论,0206为动态取消点赞,0207为动态取消喜欢,
     * 0301为发小视频,0302为小视频点赞,0303为小视频取消点赞,0304为小视频评论
     */
    private String type;

2.消息发送

@Service
public class MqMessageService {

  /*
  *交换机 tanhua.log.exchange
  *routingKey "log"+key
  */

    @Autowired
    private AmqpTemplate amqpTemplate;

    //发送日志消息
    public void sendLogMessage(Long userId,String type,String key,String busId) {
        try {
            Map map = new HashMap();
            map.put("userId",userId.toString());
            map.put("type",type);
            map.put("logTime",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            map.put("busId",busId);
            String message = JSON.toJSONString(map);
            amqpTemplate.convertAndSend("tanhua.log.exchange", "log."+key,message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }

    //发送动态审核消息
    public void sendAudiMessage(String movementId) {
        try {
            amqpTemplate.convertAndSend("tanhua.audit.exchange",  "audit.movement",movementId);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }
}

  

3.发送消息

 *在用户发生行为的地方发送对应类型的消息
 *
 *@param key      用户相关user , 动态相关movement , 小视频相关 video
 * @param busId   业务id  动态id或者视频id
 */
public void sendLogMessage(Long userId, String type, String key, String busId) 

4.保存消息到数据库

@Component
public class LogListener {

    @Autowired
    private LogMapper logMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tanhua.log.queue", durable = "true"),
            exchange = @Exchange(value = "tanhua.log.exchange", type = ExchangeTypes.TOPIC),
            key = {"log.*"}
    	)
    )
    public void listenCreate(String message) throws Exception {
        try {
            Map<String, Object> map = JSON.parseObject(message);
            //1、获取数据
            Long userId = (Long) map.get("userId");
            String date = (String) map.get("date");
            String objId = (String) map.get("objId");
            String type = (String) map.get("type");
            //2、保存到数据库
            Log log = new Log(userId, date, type);
            logMapper.insert(log);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2022-05-04 20:20  twb_QAQZ  阅读(48)  评论(0)    收藏  举报