Skip to content

Commit f9fd929

Browse files
authored
Fix containerFactory SpEL Resolution
Related to: #2809
1 parent 75a05d7 commit f9fd929

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/MultiRabbitListenerAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected String resolveMultiRabbitAdminName(RabbitListener rabbitListener) {
9595
return rlcf.getBeanName() + RabbitListenerConfigUtils.MULTI_RABBIT_ADMIN_SUFFIX;
9696
}
9797

98-
return containerFactory + RabbitListenerConfigUtils.MULTI_RABBIT_ADMIN_SUFFIX;
98+
return resolved + RabbitListenerConfigUtils.MULTI_RABBIT_ADMIN_SUFFIX;
9999
}
100100

101101
return RabbitListenerConfigUtils.RABBIT_ADMIN_BEAN_NAME;

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/MockMultiRabbitTests.java

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void multipleSimpleMessageListeners() {
6868

6969
Map<String, RabbitListenerContainerTestFactory> factories = context
7070
.getBeansOfType(RabbitListenerContainerTestFactory.class, false, false);
71-
Assertions.assertThat(factories).hasSize(3);
71+
Assertions.assertThat(factories).hasSize(4);
7272

7373
factories.values().forEach(factory -> {
7474
Assertions.assertThat(factory.getListenerContainers().size())
@@ -99,34 +99,38 @@ void testDeclarablesMatchProperRabbitAdmin() {
9999

100100
Map<String, RabbitListenerContainerTestFactory> factories = context
101101
.getBeansOfType(RabbitListenerContainerTestFactory.class, false, false);
102-
Assertions.assertThat(factories).hasSize(3);
102+
Assertions.assertThat(factories).hasSize(4);
103103

104104
BiFunction<RabbitAdmin, Declarable, Boolean> declares = (admin, dec) -> dec.getDeclaringAdmins().size() == 1
105105
&& dec.getDeclaringAdmins().contains(admin.getBeanName());
106106

107107
Map<String, AbstractExchange> exchanges = context.getBeansOfType(AbstractExchange.class, false, false)
108108
.values().stream().collect(Collectors.toMap(AbstractExchange::getName, v -> v));
109-
Assertions.assertThat(exchanges).hasSize(3);
109+
Assertions.assertThat(exchanges).hasSize(4);
110110
Assertions.assertThat(declares.apply(MultiConfig.DEFAULT_RABBIT_ADMIN, exchanges.get("testExchange"))).isTrue();
111111
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_B, exchanges.get("testExchangeB")))
112112
.isTrue();
113113
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_C, exchanges.get("testExchangeC")))
114114
.isTrue();
115+
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_D, exchanges.get("testExchangeD")))
116+
.isTrue();
115117

116118
Map<String, org.springframework.amqp.core.Queue> queues = context
117119
.getBeansOfType(org.springframework.amqp.core.Queue.class, false, false)
118120
.values().stream().collect(Collectors.toMap(org.springframework.amqp.core.Queue::getName, v -> v));
119-
Assertions.assertThat(queues).hasSize(3);
121+
Assertions.assertThat(queues).hasSize(4);
120122
Assertions.assertThat(declares.apply(MultiConfig.DEFAULT_RABBIT_ADMIN, queues.get("testQueue"))).isTrue();
121123
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_B, queues.get("testQueueB"))).isTrue();
122124
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_C, queues.get("testQueueC"))).isTrue();
125+
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_D, queues.get("testQueueD"))).isTrue();
123126

124127
Map<String, Binding> bindings = context.getBeansOfType(Binding.class, false, false)
125128
.values().stream().collect(Collectors.toMap(Binding::getRoutingKey, v -> v));
126-
Assertions.assertThat(bindings).hasSize(3);
129+
Assertions.assertThat(bindings).hasSize(4);
127130
Assertions.assertThat(declares.apply(MultiConfig.DEFAULT_RABBIT_ADMIN, bindings.get("testKey"))).isTrue();
128131
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_B, bindings.get("testKeyB"))).isTrue();
129132
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_C, bindings.get("testKeyC"))).isTrue();
133+
Assertions.assertThat(declares.apply(MultiConfig.RABBIT_ADMIN_BROKER_D, bindings.get("testKeyD"))).isTrue();
130134

131135
context.close(); // Close and stop the listeners
132136
}
@@ -180,9 +184,19 @@ void testCreationOfConnections() {
180184
Mockito.verify(MultiConfig.CONNECTION_FACTORY_BROKER_C).createConnection();
181185
Mockito.verify(MultiConfig.CONNECTION_BROKER_C).createChannel(false);
182186

187+
Mockito.verify(MultiConfig.CONNECTION_FACTORY_BROKER_D, Mockito.never()).createConnection();
188+
Mockito.verify(MultiConfig.CONNECTION_BROKER_D, Mockito.never()).createChannel(false);
189+
SimpleResourceHolder.bind(MultiConfig.ROUTING_CONNECTION_FACTORY, "brokerD");
190+
rabbitTemplate.convertAndSend("messageToBrokerD");
191+
SimpleResourceHolder.unbind(MultiConfig.ROUTING_CONNECTION_FACTORY);
192+
Mockito.verify(MultiConfig.CONNECTION_FACTORY_BROKER_D).createConnection();
193+
Mockito.verify(MultiConfig.CONNECTION_BROKER_D).createChannel(false);
194+
183195
context.close(); // Close and stop the listeners
184196
}
185197

198+
199+
186200
@Test
187201
@DisplayName("Test assignment of RabbitAdmin in the endpoint registry")
188202
void testAssignmentOfRabbitAdminInTheEndpointRegistry() {
@@ -192,7 +206,7 @@ void testAssignmentOfRabbitAdminInTheEndpointRegistry() {
192206
final RabbitListenerEndpointRegistry registry = context.getBean(RabbitListenerEndpointRegistry.class);
193207
final Collection<MessageListenerContainer> listenerContainers = registry.getListenerContainers();
194208

195-
Assertions.assertThat(listenerContainers).hasSize(3);
209+
Assertions.assertThat(listenerContainers).hasSize(4);
196210
listenerContainers.forEach(container -> {
197211
Assertions.assertThat(container).isInstanceOf(MessageListenerTestContainer.class);
198212
final MessageListenerTestContainer refContainer = (MessageListenerTestContainer) container;
@@ -228,6 +242,13 @@ public void handleItB(String body) {
228242
key = "testKeyC"))
229243
public void handleItC(String body) {
230244
}
245+
246+
@RabbitListener(containerFactory = "${broker-name:brokerD}", bindings = @QueueBinding(
247+
exchange = @Exchange("testExchangeD"),
248+
value = @Queue("testQueueD"),
249+
key = "testKeyD"))
250+
public void handleItD(String body) {
251+
}
231252
}
232253

233254
@Component
@@ -244,6 +265,10 @@ public void handleItB(String body) {
244265
@RabbitListener(queues = "testQueueC", containerFactory = "brokerC")
245266
public void handleItC(String body) {
246267
}
268+
269+
@RabbitListener(queues = "testQueueD", containerFactory = "${broker-name:brokerD}")
270+
public void handleItD(String body) {
271+
}
247272
}
248273

249274
@Configuration
@@ -254,34 +279,41 @@ static class MultiConfig {
254279
static final ConnectionFactory DEFAULT_CONNECTION_FACTORY = Mockito.mock(ConnectionFactory.class);
255280
static final ConnectionFactory CONNECTION_FACTORY_BROKER_B = Mockito.mock(ConnectionFactory.class);
256281
static final ConnectionFactory CONNECTION_FACTORY_BROKER_C = Mockito.mock(ConnectionFactory.class);
282+
static final ConnectionFactory CONNECTION_FACTORY_BROKER_D = Mockito.mock(ConnectionFactory.class);
257283

258284
static final Connection DEFAULT_CONNECTION = Mockito.mock(Connection.class);
259285
static final Connection CONNECTION_BROKER_B = Mockito.mock(Connection.class);
260286
static final Connection CONNECTION_BROKER_C = Mockito.mock(Connection.class);
287+
static final Connection CONNECTION_BROKER_D = Mockito.mock(Connection.class);
261288

262289
static final Channel DEFAULT_CHANNEL = Mockito.mock(Channel.class);
263290
static final Channel CHANNEL_BROKER_B = Mockito.mock(Channel.class);
264291
static final Channel CHANNEL_BROKER_C = Mockito.mock(Channel.class);
292+
static final Channel CHANNEL_BROKER_D = Mockito.mock(Channel.class);
265293

266294
static {
267295
final Map<Object, ConnectionFactory> targetConnectionFactories = new HashMap<>();
268296
targetConnectionFactories.put("brokerB", CONNECTION_FACTORY_BROKER_B);
269297
targetConnectionFactories.put("brokerC", CONNECTION_FACTORY_BROKER_C);
298+
targetConnectionFactories.put("brokerD", CONNECTION_FACTORY_BROKER_D);
270299
ROUTING_CONNECTION_FACTORY.setDefaultTargetConnectionFactory(DEFAULT_CONNECTION_FACTORY);
271300
ROUTING_CONNECTION_FACTORY.setTargetConnectionFactories(targetConnectionFactories);
272301

273302
Mockito.when(DEFAULT_CONNECTION_FACTORY.createConnection()).thenReturn(DEFAULT_CONNECTION);
274303
Mockito.when(CONNECTION_FACTORY_BROKER_B.createConnection()).thenReturn(CONNECTION_BROKER_B);
275304
Mockito.when(CONNECTION_FACTORY_BROKER_C.createConnection()).thenReturn(CONNECTION_BROKER_C);
305+
Mockito.when(CONNECTION_FACTORY_BROKER_D.createConnection()).thenReturn(CONNECTION_BROKER_D);
276306

277307
Mockito.when(DEFAULT_CONNECTION.createChannel(false)).thenReturn(DEFAULT_CHANNEL);
278308
Mockito.when(CONNECTION_BROKER_B.createChannel(false)).thenReturn(CHANNEL_BROKER_B);
279309
Mockito.when(CONNECTION_BROKER_C.createChannel(false)).thenReturn(CHANNEL_BROKER_C);
310+
Mockito.when(CONNECTION_BROKER_D.createChannel(false)).thenReturn(CHANNEL_BROKER_D);
280311
}
281312

282313
static final RabbitAdmin DEFAULT_RABBIT_ADMIN = new RabbitAdmin(DEFAULT_CONNECTION_FACTORY);
283314
static final RabbitAdmin RABBIT_ADMIN_BROKER_B = new RabbitAdmin(CONNECTION_FACTORY_BROKER_B);
284315
static final RabbitAdmin RABBIT_ADMIN_BROKER_C = new RabbitAdmin(CONNECTION_FACTORY_BROKER_C);
316+
static final RabbitAdmin RABBIT_ADMIN_BROKER_D = new RabbitAdmin(CONNECTION_FACTORY_BROKER_D);
285317

286318
@Bean
287319
public RabbitListenerAnnotationBeanPostProcessor postProcessor() {
@@ -307,6 +339,11 @@ public RabbitAdmin rabbitAdminBrokerC() {
307339
return RABBIT_ADMIN_BROKER_C;
308340
}
309341

342+
@Bean("brokerD-admin")
343+
public RabbitAdmin rabbitAdminBrokerD() {
344+
return RABBIT_ADMIN_BROKER_D;
345+
}
346+
310347
@Bean("defaultContainerFactory")
311348
public RabbitListenerContainerTestFactory defaultContainerFactory() {
312349
return new RabbitListenerContainerTestFactory();
@@ -322,6 +359,11 @@ public RabbitListenerContainerTestFactory containerFactoryBrokerC() {
322359
return new RabbitListenerContainerTestFactory();
323360
}
324361

362+
@Bean("brokerD")
363+
public RabbitListenerContainerTestFactory containerFactoryBrokerD() {
364+
return new RabbitListenerContainerTestFactory();
365+
}
366+
325367
@Bean
326368
public RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry() {
327369
return new RabbitListenerEndpointRegistry();

0 commit comments

Comments
 (0)