Skip to content

Commit 31906f3

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

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public MethodRedisListenerEndpoint createEndpoint(RedisListener redisListener, M
248248
MethodRedisListenerEndpoint endpoint = new MethodRedisListenerEndpoint(bean, method);
249249
endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
250250
endpoint.setId(getEndpointId(redisListener));
251-
endpoint.setTopic(redisListener.topic());
251+
endpoint.setTopic(resolve(redisListener.topic()));
252252
endpoint.setConsumes(redisListener.consumes());
253253

254254
return endpoint;

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

Lines changed: 28 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;
@@ -62,6 +65,24 @@ void registersListenerWithDefaultContainer() {
6265
assertThat(registryRef.get().isRunning()).isFalse();
6366
}
6467

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

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

141162
}
142163

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

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

0 commit comments

Comments
 (0)