Add ReactiveRedisOperations.listenToLater(…) to await subscriptions.

Original Pull Request: #2052
This commit is contained in:
Mark Paluch
2021-04-26 16:22:55 +02:00
committed by Christoph Strobl
parent fe51d78537
commit 9480851a2e
4 changed files with 111 additions and 4 deletions

View File

@@ -116,6 +116,49 @@ public interface ReactiveRedisOperations<K, V> {
*/
Flux<? extends Message<String, V>> listenTo(Topic... topics);
/**
* Subscribe to the given Redis {@code channels} and emit {@link Message messages} received for those. The
* {@link Mono} completes once the {@link Topic topic} subscriptions are registered.
*
* @param channels must not be {@literal null}.
* @return a hot sequence of {@link Message messages}.
* @since 2.6
* @see org.springframework.data.redis.listener.ReactiveRedisMessageListenerContainer#receiveLater(ChannelTopic...)
*/
default Mono<Flux<? extends Message<String, V>>> listenToChannelLater(String... channels) {
Assert.notNull(channels, "Channels must not be null!");
return listenToLater(Arrays.stream(channels).map(ChannelTopic::of).toArray(ChannelTopic[]::new));
}
/**
* Subscribe to the Redis channels matching the given {@code pattern} and emit {@link Message messages} received for
* those. The {@link Mono} completes once the {@link Topic topic} subscriptions are registered.
*
* @param patterns must not be {@literal null}.
* @return a hot sequence of {@link Message messages}.
* @since 2.6
* @see org.springframework.data.redis.listener.ReactiveRedisMessageListenerContainer#receiveLater(PatternTopic...)
*/
default Mono<Flux<? extends Message<String, V>>> listenToPatternLater(String... patterns) {
Assert.notNull(patterns, "Patterns must not be null!");
return listenToLater(Arrays.stream(patterns).map(PatternTopic::of).toArray(PatternTopic[]::new));
}
/**
* Subscribe to the Redis channels for the given {@link Topic topics} and emit {@link Message messages} received for
* those. The {@link Mono} completes once the {@link Topic topic} subscriptions are registered.
*
* @param topics must not be {@literal null}.
* @return a hot sequence of {@link Message messages}.
* @since 2.6
* @see org.springframework.data.redis.listener.ReactiveRedisMessageListenerContainer#receiveLater(Iterable,
* RedisSerializationContext.SerializationPair, RedisSerializationContext.SerializationPair)
*/
Mono<Flux<? extends Message<String, V>>> listenToLater(Topic... topics);
// -------------------------------------------------------------------------
// Methods dealing with Redis Keys
// -------------------------------------------------------------------------

View File

@@ -237,6 +237,21 @@ public class ReactiveRedisTemplate<K, V> implements ReactiveRedisOperations<K, V
.doFinally((signalType) -> container.destroyLater().subscribe());
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveRedisOperations#listenToLater(org.springframework.data.redis.listener.Topic[])
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Mono<Flux<? extends Message<String, V>>> listenToLater(Topic... topics) {
ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(getConnectionFactory());
return (Mono) container.receiveLater(Arrays.asList(topics), getSerializationContext().getStringSerializationPair(),
getSerializationContext().getValueSerializationPair()) //
.doFinally((signalType) -> container.destroyLater().subscribe());
}
// -------------------------------------------------------------------------
// Methods dealing with Redis keys
// -------------------------------------------------------------------------

View File

@@ -343,7 +343,7 @@ public class ReactiveRedisMessageListenerContainer implements DisposableBean {
* @throws InvalidDataAccessApiUsageException if {@code topics} is empty.
* @since 2.6
*/
private <C, B> Mono<Flux<Message<C, B>>> receiveLater(Iterable<? extends Topic> topics,
public <C, B> Mono<Flux<Message<C, B>>> receiveLater(Iterable<? extends Topic> topics,
SerializationPair<C> channelSerializer, SerializationPair<B> messageSerializer) {
Assert.notNull(topics, "Topics must not be null!");