Skip to content

Commit 328de94

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Replace MonoProcessor with Sinks. Original Pull Request: #2052
1 parent 58e7aa5 commit 328de94

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/main/java/org/springframework/data/redis/listener/ReactiveRedisMessageListenerContainer.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import reactor.core.publisher.Flux;
1919
import reactor.core.publisher.Mono;
20-
import reactor.core.publisher.MonoProcessor;
20+
import reactor.core.publisher.Sinks;
2121

2222
import java.nio.ByteBuffer;
2323
import java.util.Arrays;
@@ -106,7 +106,7 @@ private Mono<Void> doDestroy() {
106106
return Mono.empty();
107107
}
108108

109-
ReactiveRedisConnection connection = this.connection;
109+
ReactiveRedisConnection connection = getRequiredConnection();
110110

111111
Flux<Void> terminationSignals = null;
112112
while (!subscriptions.isEmpty()) {
@@ -136,7 +136,7 @@ private Mono<Void> doDestroy() {
136136
public Collection<ReactiveSubscription> getActiveSubscriptions() {
137137

138138
return subscriptions.entrySet().stream().filter(entry -> entry.getValue().hasRegistration())
139-
.map(entry -> entry.getKey()).collect(Collectors.toList());
139+
.map(Map.Entry::getKey).collect(Collectors.toList());
140140
}
141141

142142
/**
@@ -223,8 +223,8 @@ public <C, B> Flux<Message<C, B>> receive(Iterable<? extends Topic> topics, Seri
223223
* subscription/unsubscription and can be used for synchronization.
224224
*
225225
* @param topics the channels to subscribe.
226-
* @param channelSerializer
227-
* @param messageSerializer
226+
* @param channelSerializer serialization pair to decode the channel/pattern name.
227+
* @param messageSerializer serialization pair to decode the message body.
228228
* @param subscriptionListener listener to receive subscription/unsubscription notifications.
229229
* @return the message stream.
230230
* @see #receive(Iterable, SerializationPair, SerializationPair)
@@ -249,7 +249,7 @@ public <C, B> Flux<Message<C, B>> receive(Iterable<? extends Topic> topics, Seri
249249
}
250250

251251
return doReceive(channelSerializer, messageSerializer,
252-
connection.pubSubCommands().createSubscription(subscriptionListener), patterns,
252+
getRequiredConnection().pubSubCommands().createSubscription(subscriptionListener), patterns,
253253
channels);
254254
}
255255

@@ -261,7 +261,7 @@ private <C, B> Flux<Message<C, B>> doReceive(SerializationPair<C> channelSeriali
261261

262262
Mono<Void> subscribe = subscribe(patterns, channels, it);
263263

264-
MonoProcessor<ChannelMessage<ByteBuffer, ByteBuffer>> terminalProcessor = MonoProcessor.create();
264+
Sinks.One<Message<ByteBuffer, ByteBuffer>> terminalSink = Sinks.one();
265265
return it.receive().mergeWith(subscribe.then(Mono.defer(() -> {
266266

267267
getSubscribers(it).registered();
@@ -272,9 +272,9 @@ private <C, B> Flux<Message<C, B>> doReceive(SerializationPair<C> channelSeriali
272272
Subscribers subscribers = getSubscribers(it);
273273
if (subscribers.unregister()) {
274274
subscriptions.remove(it);
275-
it.cancel().subscribe(v -> terminalProcessor.onComplete(), terminalProcessor::onError);
275+
it.cancel().subscribe(v -> terminalSink.tryEmitEmpty(), terminalSink::tryEmitError);
276276
}
277-
}).mergeWith(terminalProcessor);
277+
}).mergeWith(terminalSink.asMono());
278278
});
279279

280280
return messageStream
@@ -303,7 +303,7 @@ private static Mono<Void> subscribe(ByteBuffer[] patterns, ByteBuffer[] channels
303303
}
304304
}
305305

306-
return subscribe;
306+
return subscribe == null ? Mono.empty() : subscribe;
307307
}
308308

309309
private boolean isActive() {
@@ -330,13 +330,12 @@ private ByteBuffer[] getTargets(Iterable<? extends Topic> topics, Class<?> class
330330
.toArray(ByteBuffer[]::new);
331331
}
332332

333-
@SuppressWarnings("unchecked")
334333
private <C, B> Message<C, B> readMessage(RedisElementReader<C> channelSerializer,
335334
RedisElementReader<B> messageSerializer, Message<ByteBuffer, ByteBuffer> message) {
336335

337336
if (message instanceof PatternMessage) {
338337

339-
PatternMessage<ByteBuffer, ByteBuffer, ByteBuffer> patternMessage = (PatternMessage) message;
338+
PatternMessage<ByteBuffer, ByteBuffer, ByteBuffer> patternMessage = (PatternMessage<ByteBuffer, ByteBuffer, ByteBuffer>) message;
340339

341340
String pattern = read(stringSerializationPair.getReader(), patternMessage.getPattern());
342341
C channel = read(channelSerializer, patternMessage.getChannel());
@@ -351,6 +350,17 @@ private <C, B> Message<C, B> readMessage(RedisElementReader<C> channelSerializer
351350
return new ChannelMessage<>(channel, body);
352351
}
353352

353+
private ReactiveRedisConnection getRequiredConnection() {
354+
355+
ReactiveRedisConnection connection = this.connection;
356+
357+
if (connection == null) {
358+
throw new IllegalStateException("Connection no longer available");
359+
}
360+
361+
return connection;
362+
}
363+
354364
private static <C> C read(RedisElementReader<C> reader, ByteBuffer buffer) {
355365

356366
try {

0 commit comments

Comments
 (0)