fix: 解决 redis mq 消息丢失问题

This commit is contained in:
gaibu 2022-12-23 10:31:41 +08:00
parent 46f3790492
commit e9645d7054

View File

@ -61,17 +61,17 @@ public class RedisPendingMessageResendJob {
listeners.forEach(listener -> { listeners.forEach(listener -> {
PendingMessagesSummary pendingMessagesSummary = ops.pending(listener.getStreamKey(), groupName); PendingMessagesSummary pendingMessagesSummary = ops.pending(listener.getStreamKey(), groupName);
// 每个消费者的pending消息数量 // 每个消费者的 pending 队列消息数量
Map<String, Long> pendingMessagesPerConsumer = pendingMessagesSummary.getPendingMessagesPerConsumer(); Map<String, Long> pendingMessagesPerConsumer = pendingMessagesSummary.getPendingMessagesPerConsumer();
pendingMessagesPerConsumer.entrySet().forEach(entry -> { pendingMessagesPerConsumer.entrySet().forEach(entry -> {
String consumerName = entry.getKey(); String consumerName = entry.getKey();
Long pendingMessageCount = entry.getValue(); Long pendingMessageCount = entry.getValue();
log.info("[processPendingMessage][消费者({}) 消息数量({})]", consumerName, pendingMessageCount); log.info("[processPendingMessage][消费者({}) 消息数量({})]", consumerName, pendingMessageCount);
// 从消费者的pending队列中读取消息 // 从消费者的 pending 队列中读取消息
List<MapRecord<String, Object, Object>> retVal = ops.read(Consumer.from(groupName, consumerName), StreamOffset.create(listener.getStreamKey(), ReadOffset.from("0"))); List<MapRecord<String, Object, Object>> records = ops.read(Consumer.from(groupName, consumerName), StreamOffset.create(listener.getStreamKey(), ReadOffset.from("0")));
if (CollUtil.isNotEmpty(retVal)) { if (CollUtil.isNotEmpty(records)) {
for (MapRecord<String, Object, Object> record : retVal) { for (MapRecord<String, Object, Object> record : records) {
// 重新投递消息 // 重新投递消息
redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord() redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord()
.ofObject(record.getValue()) // 设置内容 .ofObject(record.getValue()) // 设置内容