Skip to content

Fix: new messages are not read when a consumer is specified #3202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ErrorHandler;
import org.springframework.util.ObjectUtils;

Expand All @@ -50,6 +51,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Edsuns
* @since 2.2
*/
class DefaultStreamMessageListenerContainer<K, V extends Record<K, ?>> implements StreamMessageListenerContainer<K, V> {
Expand Down Expand Up @@ -229,8 +231,18 @@ private Function<ReadOffset, List<ByteRecord>> getReadFunction(StreamReadRequest
: this.readOptions;
Consumer consumer = consumerStreamRequest.getConsumer();

return (offset) -> template.execute((RedisCallback<List<ByteRecord>>) connection -> connection.streamCommands()
.xReadGroup(consumer, readOptions, StreamOffset.create(rawKey, offset)));
return (offset) -> {
List<ByteRecord> records = template.execute((RedisCallback<List<ByteRecord>>) connection -> connection.streamCommands()
.xReadGroup(consumer, readOptions, StreamOffset.create(rawKey, offset)));
if (CollectionUtils.isEmpty(records) && !ReadOffset.lastConsumed().equals(offset)) {
// see https://redis.io/docs/latest/commands/xreadgroup/
// if ID in XREADGROUP command is other than >, new messages won't be read
// so reads new messages here
return template.execute((RedisCallback<List<ByteRecord>>) connection -> connection.streamCommands()
.xReadGroup(consumer, readOptions, StreamOffset.create(rawKey, ReadOffset.lastConsumed())));
}
return records;
};
}

return (offset) -> template.execute((RedisCallback<List<ByteRecord>>) connection -> connection.streamCommands()
Expand Down