Skip to content

Commit 5bccee3

Browse files
committed
Resolve @RedisListener topic placeholders.
Closes #3393 Signed-off-by: Dongliang Xie <dragonfsky@gmail.com>
1 parent 3e5d97a commit 5bccee3

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/main/java/org/springframework/data/redis/annotation/RedisListenerAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Ilyass Bougati
7070
* @author Mark Paluch
7171
* @author Christoph Strobl
72+
* @author Dongliang Xie
7273
* @since 4.1
7374
* @see RedisListener
7475
*/
@@ -248,7 +249,7 @@ public MethodRedisListenerEndpoint createEndpoint(RedisListener redisListener, M
248249
MethodRedisListenerEndpoint endpoint = new MethodRedisListenerEndpoint(bean, method);
249250
endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
250251
endpoint.setId(getEndpointId(redisListener));
251-
endpoint.setTopic(redisListener.topic());
252+
endpoint.setTopic(resolve(redisListener.topic()));
252253
endpoint.setConsumes(redisListener.consumes());
253254

254255
return endpoint;

src/test/java/org/springframework/data/redis/annotation/RedisListenerAnnotationBeanPostProcessorIntegrationTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
import static org.mockito.ArgumentMatchers.*;
2020
import static org.mockito.Mockito.*;
2121

22+
import java.util.Map;
2223
import java.util.concurrent.atomic.AtomicReference;
2324
import java.util.function.Consumer;
2425

2526
import org.junit.jupiter.api.Test;
27+
import org.mockito.ArgumentCaptor;
2628

2729
import org.springframework.beans.factory.BeanCreationException;
2830
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2931
import org.springframework.context.ApplicationContext;
3032
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3133
import org.springframework.context.annotation.Bean;
3234
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.core.env.MapPropertySource;
3336
import org.springframework.data.redis.config.RedisListenerConfigUtils;
3437
import org.springframework.data.redis.config.RedisListenerEndpointRegistry;
3538
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@@ -40,6 +43,7 @@
4043
*
4144
* @author Ilyass Bougati
4245
* @author Mark Paluch
46+
* @author Dongliang Xie
4347
*/
4448
class RedisListenerAnnotationBeanPostProcessorIntegrationTests {
4549

@@ -62,6 +66,24 @@ void registersListenerWithDefaultContainer() {
6266
assertThat(registryRef.get().isRunning()).isFalse();
6367
}
6468

69+
@Test // GH-3393
70+
void resolvesListenerTopicPlaceholder() {
71+
72+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
73+
context.getEnvironment().getPropertySources()
74+
.addFirst(new MapPropertySource("redis-listener-test", Map.of("app.my-channel", "my-channel")));
75+
context.register(DefaultConfig.class, PlaceholderTopicService.class);
76+
context.refresh();
77+
78+
RedisMessageListenerContainer container = context.getBean("redisMessageListenerContainer",
79+
RedisMessageListenerContainer.class);
80+
ArgumentCaptor<Topic> topicCaptor = ArgumentCaptor.forClass(Topic.class);
81+
82+
verify(container).addMessageListener(any(), topicCaptor.capture());
83+
assertThat(topicCaptor.getValue().getTopic()).isEqualTo("my-channel");
84+
}
85+
}
86+
6587
@Test // GH-3340
6688
void registersListenerWithNamedContainer() {
6789

@@ -140,6 +162,13 @@ public void handle(String msg) {}
140162

141163
}
142164

165+
static class PlaceholderTopicService {
166+
167+
@RedisListener(topic = "${app.my-channel}")
168+
public void handle(String msg) {}
169+
170+
}
171+
143172
static class UnnamedContainerService {
144173

145174
@RedisListener(topic = "test-topic", container = "")

0 commit comments

Comments
 (0)