Spring Data Redis Streams,无法弄清楚我未确认的消息发生了什么?

Spring Data Redis Streams, Cannot figure out what is happening to my unacknowleded messages?

我正在使用以下代码使用 Spring Data Redis 消费者组消费 Redis 流,但即使我已经注释掉确认命令,我的消息在服务器重启后也不会被重新读取.

我希望如果我没有确认该消息,它应该在服务器被终止并重新启动时重新读取。我在这里错过了什么?

@Bean
@Autowired
public StreamMessageListenerContainer eventStreamPersistenceListenerContainerTwo(RedisConnectionFactory streamRedisConnectionFactory, RedisTemplate streamRedisTemplate) {

        StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, MapRecord<String, String, String>> containerOptions = StreamMessageListenerContainer.StreamMessageListenerContainerOptions
                        .builder().pollTimeout(Duration.ofMillis(100)).build();

        StreamMessageListenerContainer<String, MapRecord<String, String, String>> container = StreamMessageListenerContainer.create(streamRedisConnectionFactory,
                        containerOptions);

        container.receive(Consumer.from("my-group", "my-consumer"),
                        StreamOffset.create("event-stream", ReadOffset.latest()),
                        message -> {
                                System.out.println("MessageId: " + message.getId());
                                System.out.println("Stream: " + message.getStream());
                                System.out.println("Body: " + message.getValue());
                                //streamRedisTemplate.opsForStream().acknowledge("my-group", message);
                        });

        container.start();

        return container;
}

在阅读了关于流如何工作的 Redis 文档后,我想出了以下方法来自动处理任何未确认但先前已为消费者传递的消息:

// Check for any previously unacknowledged messages that were delivered to this consumer.
log.info("STREAM - Checking for previously unacknowledged messages for " + this.getClass().getSimpleName() + " event stream listener.");
String offset = "0";
while ((offset = processUnacknowledgedMessage(offset)) != null) {
        log.info("STREAM - Finished processing one unacknowledged message for " + this.getClass().getSimpleName() + " event stream listener: " + offset);
}
log.info("STREAM - Finished checking for previously unacknowledged messages for " + this.getClass().getSimpleName() + " event stream listener.");

以及处理消息的方法:

/**
 * Processes, and acknowledges the next previously delivered message, beginning
 * at the given message id offset.
 *
 * @param offset The last read message id offset.
 * @return The message that was just processed, or null if there are no more messages.
 */
public String processUnacknowledgedMessage(String offset) {
        List<MapRecord> messages = streamRedisTemplate.opsForStream().read(Consumer.from(groupName(), consumerName()),
                        StreamReadOptions.empty().noack().count(1),
                        StreamOffset.create(streamKey(), ReadOffset.from(offset)));
        String lastMessageId = null;
        for (MapRecord message : messages) {
                if (log.isDebugEnabled()) log.debug(String.format("STREAM - Processing event(%s) from stream(%s) during startup: %s", message.getId(), message.getStream(), message.getValue()));
                processRecord(message);
                if (log.isDebugEnabled()) log.debug(String.format("STREAM - Finished processing event(%s) from stream(%s) during startup.", message.getId(), message.getStream()));
                streamRedisTemplate.opsForStream().acknowledge(groupName(), message);
                lastMessageId = message.getId().getValue();
        }
        return lastMessageId;
}