feat(count): 实现笔记收藏计数功能
- 新增笔记收藏/取消收藏 MQ 消费者,处理收藏计数逻辑 - 新增笔记收藏数落库消费者,实现批量更新数据库 - 新增收藏类型枚举和 DTO 类,用于消息传递与解析 - 修改 MQ 消费组名称前缀统一为 han_note_group_ - 新增 Redis 收藏总数字段常量及更新逻辑 - 扩展 NoteCountDOMapper 支持收藏数插入或更新操作 - 在 XML 映射文件中新增对应 SQL 插入语句 - 完善 MQ 常量定义,增加收藏相关主题常量
This commit is contained in:
@@ -8,10 +8,14 @@ import com.hanserwei.hannote.note.biz.domain.mapper.NoteCollectionDOMapper;
|
||||
import com.hanserwei.hannote.note.biz.model.dto.CollectUnCollectNoteMqDTO;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.client.producer.SendCallback;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.spring.annotation.ConsumeMode;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -21,7 +25,7 @@ import java.util.Objects;
|
||||
@Component
|
||||
@Slf4j
|
||||
@RocketMQMessageListener(
|
||||
consumerGroup = "han_note_" + MQConstants.TOPIC_COLLECT_OR_UN_COLLECT,
|
||||
consumerGroup = "han_note_group_" + MQConstants.TOPIC_COLLECT_OR_UN_COLLECT,
|
||||
topic = MQConstants.TOPIC_COLLECT_OR_UN_COLLECT,
|
||||
consumeMode = ConsumeMode.ORDERLY
|
||||
)
|
||||
@@ -31,6 +35,8 @@ public class CollectUnCollectNoteConsumer implements RocketMQListener<Message> {
|
||||
private final RateLimiter rateLimiter = RateLimiter.create(5000);
|
||||
@Resource
|
||||
private NoteCollectionDOMapper noteCollectionDOMapper;
|
||||
@Resource
|
||||
private RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message) {
|
||||
@@ -85,7 +91,27 @@ public class CollectUnCollectNoteConsumer implements RocketMQListener<Message> {
|
||||
// 取消收藏:记录更新
|
||||
int count = noteCollectionDOMapper.update2UnCollectByUserIdAndNoteId(noteCollectionDO);
|
||||
|
||||
// TODO: 发送计数 MQ
|
||||
if (count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新数据库成功后,发送计数 MQ
|
||||
org.springframework.messaging.Message<String> message = MessageBuilder.withPayload(bodyJsonStr)
|
||||
.build();
|
||||
|
||||
// 异步发送 MQ 消息
|
||||
rocketMQTemplate.asyncSend(MQConstants.TOPIC_COUNT_NOTE_COLLECT, message, new SendCallback() {
|
||||
@Override
|
||||
public void onSuccess(SendResult sendResult) {
|
||||
log.info("==> 【计数: 笔记取消收藏】MQ 发送成功,SendResult: {}", sendResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable throwable) {
|
||||
log.error("==> 【计数: 笔记取消收藏】MQ 发送异常: ", throwable);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,6 +145,28 @@ public class CollectUnCollectNoteConsumer implements RocketMQListener<Message> {
|
||||
// 添加或更新笔记收藏记录
|
||||
boolean isSuccess = noteCollectionDOMapper.insertOrUpdate(noteCollectionDO);
|
||||
|
||||
// TODO: 发送计数 MQ
|
||||
if (!isSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送计数 MQ
|
||||
|
||||
// 更新数据库成功后,发送计数 MQ
|
||||
org.springframework.messaging.Message<String> message = MessageBuilder.withPayload(bodyJsonStr)
|
||||
.build();
|
||||
|
||||
// 异步发送 MQ 消息
|
||||
// 异步发送 MQ 消息
|
||||
rocketMQTemplate.asyncSend(MQConstants.TOPIC_COUNT_NOTE_COLLECT, message, new SendCallback() {
|
||||
@Override
|
||||
public void onSuccess(SendResult sendResult) {
|
||||
log.info("==> 【计数: 笔记收藏】MQ 发送成功,SendResult: {}", sendResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable throwable) {
|
||||
log.error("==> 【计数: 笔记收藏】MQ 发送异常: ", throwable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.Objects;
|
||||
@SuppressWarnings({"UnstableApiUsage"})
|
||||
@Component
|
||||
@RocketMQMessageListener(
|
||||
consumerGroup = "han_note_" + MQConstants.TOPIC_LIKE_OR_UNLIKE,
|
||||
consumerGroup = "han_note_group_" + MQConstants.TOPIC_LIKE_OR_UNLIKE,
|
||||
topic = MQConstants.TOPIC_LIKE_OR_UNLIKE,
|
||||
consumeMode = ConsumeMode.ORDERLY// 顺序消费
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user