Add support for SubscriptionListener using ReactiveRedisMessageListenerContainer.

Original Pull Request: #2052
This commit is contained in:
Mark Paluch
2021-04-26 13:59:02 +02:00
committed by Christoph Strobl
parent 0f1c59813e
commit 58e7aa57a2
11 changed files with 323 additions and 22 deletions

View File

@@ -37,7 +37,18 @@ public interface ReactivePubSubCommands {
*
* @return the subscription.
*/
Mono<ReactiveSubscription> createSubscription();
default Mono<ReactiveSubscription> createSubscription() {
return createSubscription(SubscriptionListener.EMPTY);
}
/**
* Creates a subscription for this connection. Connections can have multiple {@link ReactiveSubscription}s.
*
* @param subscriptionListener the subscription listener to listen for subscription confirmations.
* @return the subscription.
* @since 2.6
*/
Mono<ReactiveSubscription> createSubscription(SubscriptionListener subscriptionListener);
/**
* Publishes the given {@code message} to the given {@code channel}.

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.pubsub.RedisPubSubListener;
import java.nio.ByteBuffer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Wrapper around {@link RedisPubSubListener} that converts {@link ByteBuffer} into {@code byte[]}.
*
* @author Mark Paluch
* @since 2.6
*/
class LettuceByteBufferPubSubListenerWrapper implements RedisPubSubListener<ByteBuffer, ByteBuffer> {
private final RedisPubSubListener<byte[], byte[]> delegate;
LettuceByteBufferPubSubListenerWrapper(RedisPubSubListener<byte[], byte[]> delegate) {
Assert.notNull(delegate, "RedisPubSubListener must not be null!");
this.delegate = delegate;
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#message(java.lang.Object, java.lang.Object)
*/
public void message(ByteBuffer channel, ByteBuffer message) {
delegate.message(getBytes(channel), getBytes(message));
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#message(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public void message(ByteBuffer pattern, ByteBuffer channel, ByteBuffer message) {
delegate.message(getBytes(channel), getBytes(message), getBytes(pattern));
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#subscribed(java.lang.Object, long)
*/
public void subscribed(ByteBuffer channel, long count) {
delegate.subscribed(getBytes(channel), count);
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#psubscribed(java.lang.Object, long)
*/
public void psubscribed(ByteBuffer pattern, long count) {
delegate.psubscribed(getBytes(pattern), count);
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#unsubscribed(java.lang.Object, long)
*/
public void unsubscribed(ByteBuffer channel, long count) {
delegate.unsubscribed(getBytes(channel), count);
}
/*
* (non-Javadoc)
* @see io.lettuce.core.pubsub.RedisPubSubListener#punsubscribed(java.lang.Object, long)
*/
public void punsubscribed(ByteBuffer pattern, long count) {
delegate.punsubscribed(getBytes(pattern), count);
}
/**
* Extract a byte array from {@link ByteBuffer} without consuming it.
*
* @param byteBuffer must not be {@literal null}.
* @return
*/
private static byte[] getBytes(@Nullable ByteBuffer byteBuffer) {
if (byteBuffer == null) {
return new byte[0];
}
if (byteBuffer.hasArray()) {
return byteBuffer.array();
}
byteBuffer.mark();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
byteBuffer.reset();
return bytes;
}
}

View File

@@ -33,12 +33,13 @@ class LettuceMessageListener implements RedisPubSubListener<byte[], byte[]> {
private final MessageListener listener;
private final SubscriptionListener subscriptionListener;
LettuceMessageListener(MessageListener listener) {
LettuceMessageListener(MessageListener listener, SubscriptionListener subscriptionListener) {
Assert.notNull(listener, "MessageListener must not be null!");
Assert.notNull(subscriptionListener, "SubscriptionListener must not be null!");
this.listener = listener;
this.subscriptionListener = listener instanceof SubscriptionListener ? (SubscriptionListener) listener
: SubscriptionListener.EMPTY;
this.subscriptionListener = subscriptionListener;
}
/*

View File

@@ -23,9 +23,11 @@ import java.nio.ByteBuffer;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.ReactivePubSubCommands;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.SubscriptionListener;
import org.springframework.util.Assert;
/**
@@ -43,13 +45,13 @@ class LettuceReactivePubSubCommands implements ReactivePubSubCommands {
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactivePubSubCommands#createSubscription()
* @see org.springframework.data.redis.connection.ReactivePubSubCommands#createSubscription(org.springframework.data.redis.connection.SubscriptionListener)
*/
@Override
public Mono<ReactiveSubscription> createSubscription() {
public Mono<ReactiveSubscription> createSubscription(SubscriptionListener listener) {
return connection.getPubSubConnection()
.map(pubSubConnection -> new LettuceReactiveSubscription(pubSubConnection.reactive(),
.map(pubSubConnection -> new LettuceReactiveSubscription(listener, pubSubConnection,
connection.translateException()));
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.reactive.RedisPubSubReactiveCommands;
import reactor.core.Disposable;
import reactor.core.publisher.ConnectableFlux;
@@ -33,6 +34,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.data.redis.connection.SubscriptionListener;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -46,15 +48,23 @@ import org.springframework.util.ObjectUtils;
*/
class LettuceReactiveSubscription implements ReactiveSubscription {
private final LettuceByteBufferPubSubListenerWrapper listener;
private final StatefulRedisPubSubConnection<ByteBuffer, ByteBuffer> connection;
private final RedisPubSubReactiveCommands<ByteBuffer, ByteBuffer> commands;
private final State patternState;
private final State channelState;
LettuceReactiveSubscription(RedisPubSubReactiveCommands<ByteBuffer, ByteBuffer> commands,
LettuceReactiveSubscription(SubscriptionListener subscriptionListener,
StatefulRedisPubSubConnection<ByteBuffer, ByteBuffer> connection,
Function<Throwable, Throwable> exceptionTranslator) {
this.commands = commands;
this.listener = new LettuceByteBufferPubSubListenerWrapper(
new LettuceMessageListener((messages, pattern) -> {}, subscriptionListener));
this.connection = connection;
this.commands = connection.reactive();
connection.addListener(listener);
this.patternState = new State(exceptionTranslator);
this.channelState = new State(exceptionTranslator);
}
@@ -176,7 +186,12 @@ class LettuceReactiveSubscription implements ReactiveSubscription {
channelState.terminate();
patternState.terminate();
return Mono.empty();
// this is to ensure completion of the futures and result processing. Since we're unsubscribing first, we expect
// that we receive pub/sub confirmations before the PING response.
return commands.ping().then(Mono.fromRunnable(() -> {
connection.removeListener(listener);
}));
}));
}

View File

@@ -24,6 +24,7 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.SubscriptionListener;
import org.springframework.data.redis.connection.util.AbstractSubscription;
/**
@@ -58,7 +59,8 @@ public class LettuceSubscription extends AbstractSubscription {
super(listener);
this.connection = pubsubConnection;
this.listener = new LettuceMessageListener(listener);
this.listener = new LettuceMessageListener(listener,
listener instanceof SubscriptionListener ? (SubscriptionListener) listener : SubscriptionListener.EMPTY);
this.connectionProvider = connectionProvider;
this.pubsub = connection.sync();
this.pubSubAsync = connection.async();

View File

@@ -40,6 +40,7 @@ import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.data.redis.connection.ReactiveSubscription.ChannelMessage;
import org.springframework.data.redis.connection.ReactiveSubscription.Message;
import org.springframework.data.redis.connection.ReactiveSubscription.PatternMessage;
import org.springframework.data.redis.connection.SubscriptionListener;
import org.springframework.data.redis.serializer.RedisElementReader;
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -178,21 +179,65 @@ public class ReactiveRedisMessageListenerContainer implements DisposableBean {
}
/**
* Subscribe to one or more {@link Topic}s and receive a stream of {@link ChannelMessage} The stream may contain
* Subscribe to one or more {@link Topic}s and receive a stream of {@link ChannelMessage}. The stream may contain
* {@link PatternMessage} if subscribed to patterns. Messages, and channel names are serialized/deserialized using the
* given {@code channelSerializer} and {@code messageSerializer}. The message stream subscribes lazily to the Redis
* channels and unsubscribes if the {@link org.reactivestreams.Subscription} is
* {@link org.reactivestreams.Subscription#cancel() cancelled}.
*
* @param topics the channels to subscribe.
* @param topics the channels/patterns to subscribe.
* @param subscriptionListener listener to receive subscription/unsubscription notifications.
* @return the message stream.
* @throws InvalidDataAccessApiUsageException if {@code patternTopics} is empty.
* @see #receive(Iterable, SerializationPair, SerializationPair)
* @since 2.6
*/
public Flux<Message<String, String>> receive(Iterable<? extends Topic> topics,
SubscriptionListener subscriptionListener) {
return receive(topics, stringSerializationPair, stringSerializationPair, subscriptionListener);
}
/**
* Subscribe to one or more {@link Topic}s and receive a stream of {@link ChannelMessage}. The stream may contain
* {@link PatternMessage} if subscribed to patterns. Messages, and channel names are serialized/deserialized using the
* given {@code channelSerializer} and {@code messageSerializer}. The message stream subscribes lazily to the Redis
* channels and unsubscribes if the {@link org.reactivestreams.Subscription} is
* {@link org.reactivestreams.Subscription#cancel() cancelled}.
*
* @param topics the channels/patterns to subscribe.
* @return the message stream.
* @see #receive(Iterable, SerializationPair, SerializationPair)
* @throws InvalidDataAccessApiUsageException if {@code topics} is empty.
*/
public <C, B> Flux<Message<C, B>> receive(Iterable<? extends Topic> topics, SerializationPair<C> channelSerializer,
SerializationPair<B> messageSerializer) {
return receive(topics, channelSerializer, messageSerializer, SubscriptionListener.EMPTY);
}
/**
* Subscribe to one or more {@link Topic}s and receive a stream of {@link ChannelMessage}. The stream may contain
* {@link PatternMessage} if subscribed to patterns. Messages, and channel names are serialized/deserialized using the
* given {@code channelSerializer} and {@code messageSerializer}. The message stream subscribes lazily to the Redis
* channels and unsubscribes if the {@link org.reactivestreams.Subscription} is
* {@link org.reactivestreams.Subscription#cancel() cancelled}. {@link SubscriptionListener} is notified upon
* subscription/unsubscription and can be used for synchronization.
*
* @param topics the channels to subscribe.
* @param channelSerializer
* @param messageSerializer
* @param subscriptionListener listener to receive subscription/unsubscription notifications.
* @return the message stream.
* @see #receive(Iterable, SerializationPair, SerializationPair)
* @throws InvalidDataAccessApiUsageException if {@code topics} is empty.
* @since 2.6
*/
public <C, B> Flux<Message<C, B>> receive(Iterable<? extends Topic> topics, SerializationPair<C> channelSerializer,
SerializationPair<B> messageSerializer, SubscriptionListener subscriptionListener) {
Assert.notNull(topics, "Topics must not be null!");
Assert.notNull(channelSerializer, "Channel serializer must not be null!");
Assert.notNull(messageSerializer, "Message serializer must not be null!");
Assert.notNull(subscriptionListener, "SubscriptionListener must not be null!");
verifyConnection();
@@ -203,7 +248,8 @@ public class ReactiveRedisMessageListenerContainer implements DisposableBean {
throw new InvalidDataAccessApiUsageException("No channels or patterns to subscribe to.");
}
return doReceive(channelSerializer, messageSerializer, connection.pubSubCommands().createSubscription(), patterns,
return doReceive(channelSerializer, messageSerializer,
connection.pubSubCommands().createSubscription(subscriptionListener), patterns,
channels);
}
@@ -226,7 +272,7 @@ public class ReactiveRedisMessageListenerContainer implements DisposableBean {
Subscribers subscribers = getSubscribers(it);
if (subscribers.unregister()) {
subscriptions.remove(it);
it.unsubscribe().subscribe(v -> terminalProcessor.onComplete(), terminalProcessor::onError);
it.cancel().subscribe(v -> terminalProcessor.onComplete(), terminalProcessor::onError);
}
}).mergeWith(terminalProcessor);
});