diff --git a/yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/job/RedisPendingMessageResendJob.java b/yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/job/RedisPendingMessageResendJob.java
index ad0ae833d..4a0fbdfb2 100644
--- a/yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/job/RedisPendingMessageResendJob.java
+++ b/yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/job/RedisPendingMessageResendJob.java
@@ -29,8 +29,13 @@ public class RedisPendingMessageResendJob {
private final RedisMQTemplate redisTemplate;
private final String groupName;
private final RedissonClient redissonClient;
-
- private final long expireTime = 1000 * 60;
+ /**
+ * 消息超时时间,默认5分钟
+ *
超时的消息才会被重新投递
+ * 由于定时任务1分钟一次,消息超时后不会被立即重投,
+ * 极端情况下消息5分钟过期后,再等1分钟才会被扫瞄到
+ */
+ private final long expireTime = 5 * 60;
/**
* 一分钟执行一次,这里选择每分钟的35秒执行,是为了避免整点任务过多的问题
@@ -58,23 +63,28 @@ public class RedisPendingMessageResendJob {
Map pendingMessagesPerConsumer = pendingMessagesSummary.getPendingMessagesPerConsumer();
pendingMessagesPerConsumer.forEach((consumerName, pendingMessageCount) -> {
log.info("[processPendingMessage][消费者({}) 消息数量({})]", consumerName, pendingMessageCount);
-
+ // 每个消费者的 pending消息的详情信息
PendingMessages pendingMessages = ops.pending(listener.getStreamKey(), Consumer.from(groupName, consumerName), Range.unbounded(), pendingMessageCount);
if(pendingMessages.isEmpty()){
return;
}
for (PendingMessage pendingMessage : pendingMessages) {
- if(pendingMessage.getElapsedTimeSinceLastDelivery().toMillis() - expireTime >= 0){
- List> records = ops.range(listener.getStreamKey(), Range.of(Range.Bound.inclusive(pendingMessage.getIdAsString()), Range.Bound.inclusive(pendingMessage.getIdAsString())));
- if(!CollUtil.isEmpty(records)){
- // 重新投递消息
- redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord()
- .ofObject(records.get(0).getValue()) // 设置内容
- .withStreamKey(listener.getStreamKey()));
- // ack 消息消费完成
- redisTemplate.getRedisTemplate().opsForStream().acknowledge(groupName, records.get(0));
- }
+ // 获取消息上一次传递到 consumer 的时间,
+ long lastDelivery = pendingMessage.getElapsedTimeSinceLastDelivery().getSeconds();
+ if(lastDelivery < expireTime){
+ continue;
}
+ // 获取指定id的消息体
+ List> records = ops.range(listener.getStreamKey(), Range.of(Range.Bound.inclusive(pendingMessage.getIdAsString()), Range.Bound.inclusive(pendingMessage.getIdAsString())));
+ if(CollUtil.isEmpty(records)){
+ continue;
+ }
+ // 重新投递消息
+ redisTemplate.getRedisTemplate().opsForStream().add(StreamRecords.newRecord()
+ .ofObject(records.get(0).getValue()) // 设置内容
+ .withStreamKey(listener.getStreamKey()));
+ // ack 消息消费完成
+ redisTemplate.getRedisTemplate().opsForStream().acknowledge(groupName, records.get(0));
}
});
});