Skip to content

Commit a87540e

Browse files
mp911dechristophstrobl
authored andcommitted
Use proper subscribe methods and fix nullability assumptions.
See: #964 Original Pull Request: #2256
1 parent 431dfc2 commit a87540e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public void addMessageListener(MessageListener listener, Topic topic) {
596596
* @param listener message listener
597597
* @param topics message listener topics
598598
*/
599-
public void removeMessageListener(MessageListener listener, Collection<? extends Topic> topics) {
599+
public void removeMessageListener(@Nullable MessageListener listener, Collection<? extends Topic> topics) {
600600
removeListener(listener, topics);
601601
}
602602

@@ -610,7 +610,7 @@ public void removeMessageListener(MessageListener listener, Collection<? extends
610610
* @param listener message listener
611611
* @param topic message topic
612612
*/
613-
public void removeMessageListener(MessageListener listener, Topic topic) {
613+
public void removeMessageListener(@Nullable MessageListener listener, Topic topic) {
614614
removeMessageListener(listener, Collections.singleton(topic));
615615
}
616616

@@ -621,6 +621,8 @@ public void removeMessageListener(MessageListener listener, Topic topic) {
621621
* @param listener message listener
622622
*/
623623
public void removeMessageListener(MessageListener listener) {
624+
625+
Assert.notNull(listener, "MessageListener must not be null");
624626
removeMessageListener(listener, Collections.emptySet());
625627
}
626628

@@ -717,12 +719,11 @@ else if (topic instanceof PatternTopic) {
717719
}
718720
}
719721

720-
private void removeListener(MessageListener listener, Collection<? extends Topic> topics) {
722+
private void removeListener(@Nullable MessageListener listener, Collection<? extends Topic> topics) {
721723

722-
Assert.notNull(listener, "MessageListener must not be null");
723724
Assert.notNull(topics, "Topics must not be null");
724725

725-
if (listenerTopics.get(listener) == null) {
726+
if (listener != null && listenerTopics.get(listener) == null) {
726727
// Listener not subscribed
727728
return;
728729
}
@@ -1197,7 +1198,11 @@ void doSubscribe(RedisConnection connection, Collection<byte[]> patterns, Collec
11971198
}
11981199

11991200
if (!channels.isEmpty()) {
1200-
connection.subscribe(synchronizingMessageListener, channels.toArray(new byte[0][]));
1201+
if (patterns.isEmpty()) {
1202+
connection.subscribe(synchronizingMessageListener, channels.toArray(new byte[0][]));
1203+
} else {
1204+
subscribeChannel(channels.toArray(new byte[0][]));
1205+
}
12011206
}
12021207
}
12031208

src/test/java/org/springframework/data/redis/listener/PubSubResubscribeTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,10 @@ void testInitializeContainerWithMultipleTopicsIncludingPattern() throws Exceptio
210210
container.stop();
211211

212212
String uniqueChannel = "random-" + UUID.randomUUID();
213-
PubSubAwaitUtil.runAndAwaitPatternSubscription(template.getConnectionFactory(), () -> {
214213

215-
container.addMessageListener(adapter,
216-
Arrays.asList(new Topic[] { new ChannelTopic(uniqueChannel), new PatternTopic("s*") }));
217-
container.start();
218-
});
214+
container.addMessageListener(adapter,
215+
Arrays.asList(new Topic[] { new ChannelTopic(uniqueChannel), new PatternTopic("s*") }));
216+
container.start();
219217

220218
// timing: There's currently no other way to synchronize
221219
// than to hope the subscribe/unsubscribe are executed within the time.

0 commit comments

Comments
 (0)