DATAREDIS-1079 - Fix StreamMessageListenerContainer autoAck on receive.

Original pull request: #508.
This commit is contained in:
Christoph Strobl
2020-01-24 11:12:00 +01:00
committed by Mark Paluch
parent 75fcb0ec3d
commit fccaeb23e9
6 changed files with 93 additions and 20 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
* Options for reading messages from a Redis Stream.
*
* @author Mark Paluch
* @author Christoph Strobl
* @see 2.2
*/
@EqualsAndHashCode
@@ -57,7 +58,7 @@ public class StreamReadOptions {
}
/**
* Disable auto-acknowledgement when reading in the context of a consumer group.
* Enable auto-acknowledgement by setting the {@code NOACK} flag when reading in the context of a consumer group.
*
* @return {@link StreamReadOptions} with {@code noack} applied.
*/
@@ -65,6 +66,15 @@ public class StreamReadOptions {
return new StreamReadOptions(block, count, true);
}
/**
* Enable auto-acknowledgement by setting the {@code NOACK} flag when reading in the context of a consumer group.
*
* @return new instance of {@link StreamReadOptions} with {@code noack} applied.
*/
public StreamReadOptions autoAcknowledge() {
return new StreamReadOptions(block, count, true);
}
/**
* Use a blocking read and supply the {@link Duration timeout} after which the call will terminate if no message was
* read.

View File

@@ -45,6 +45,7 @@ import org.springframework.util.ErrorHandler;
* This message container creates long-running tasks that are executed on {@link Executor}.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.2
*/
class DefaultStreamMessageListenerContainer<K, V extends Record<K, ?>> implements StreamMessageListenerContainer<K, V> {
@@ -223,7 +224,7 @@ class DefaultStreamMessageListenerContainer<K, V extends Record<K, ?>> implement
ConsumerStreamReadRequest<K> consumerStreamRequest = (ConsumerStreamReadRequest<K>) streamRequest;
StreamReadOptions readOptions = consumerStreamRequest.isAutoAck() ? this.readOptions : this.readOptions.noack();
StreamReadOptions readOptions = consumerStreamRequest.isAutoAck() ? this.readOptions.autoAcknowledge() : this.readOptions;
Consumer consumer = consumerStreamRequest.getConsumer();
if (this.containerOptions.getHashMapper() != null) {

View File

@@ -47,6 +47,7 @@ import org.springframework.data.redis.serializer.RedisSerializationContext;
* Default implementation of {@link StreamReceiver}.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.2
*/
class DefaultStreamReceiver<K, V extends Record<K, ?>> implements StreamReceiver<K, V> {
@@ -134,7 +135,7 @@ class DefaultStreamReceiver<K, V extends Record<K, ?>> implements StreamReceiver
}
BiFunction<K, ReadOffset, Flux<? extends Record<?, ?>>> readFunction = getConsumeReadFunction(consumer,
this.readOptions);
this.readOptions.autoAcknowledge());
return Flux.defer(() -> {
@@ -157,7 +158,7 @@ class DefaultStreamReceiver<K, V extends Record<K, ?>> implements StreamReceiver
}
BiFunction<K, ReadOffset, Flux<? extends Record<?, ?>>> readFunction = getConsumeReadFunction(consumer,
this.readOptions.noack());
this.readOptions);
return Flux.defer(() -> {
PollState pollState = PollState.consumer(consumer, streamOffset.getOffset());

View File

@@ -104,6 +104,7 @@ import org.springframework.util.ErrorHandler;
* </pre>
*
* @author Mark Paluch
* @author Christoph Strobl
* @param <K> Stream key and Stream field type.
* @param <V> Stream value type.
* @since 2.2
@@ -196,7 +197,7 @@ public interface StreamMessageListenerContainer<K, V extends Record<K, ?>> exten
* @see ReadOffset#lastConsumed()
*/
default Subscription receive(Consumer consumer, StreamOffset<K> streamOffset, StreamListener<K, V> listener) {
return register(StreamReadRequest.builder(streamOffset).consumer(consumer).build(), listener);
return register(StreamReadRequest.builder(streamOffset).consumer(consumer).autoAck(false).build(), listener);
}
/**