Skip to content

Commit 3fed444

Browse files
garyrussellartembilan
authored andcommitted
GH-1415: Fix Use of Routing Connection Factory
Resolves #1415 The `sendConnectionFactorySelectorExpression` was being used for receive methods instead of `receiveConnectionFactorySelectorExpression`. **cherry-pick to 2.3.x, 2.2.x**
1 parent cf96793 commit 3fed444

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,10 +1088,10 @@ private ConnectionFactory obtainTargetConnectionFactory(Expression expression, O
10881088
(AbstractRoutingConnectionFactory) getConnectionFactory();
10891089
Object lookupKey;
10901090
if (rootObject != null) {
1091-
lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext, rootObject);
1091+
lookupKey = expression.getValue(this.evaluationContext, rootObject);
10921092
}
10931093
else {
1094-
lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext);
1094+
lookupKey = expression.getValue(this.evaluationContext);
10951095
}
10961096
if (lookupKey != null) {
10971097
ConnectionFactory connectionFactory = routingConnectionFactory.getTargetConnectionFactory(lookupKey);

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -371,24 +371,33 @@ public void testNoListenerAllowed2() {
371371
@SuppressWarnings("unchecked")
372372
public void testRoutingConnectionFactory() throws Exception {
373373
org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory1 =
374-
Mockito.mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
374+
mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
375375
org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory2 =
376-
Mockito.mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
376+
mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
377+
org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory3 =
378+
mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
379+
org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory4 =
380+
mock(org.springframework.amqp.rabbit.connection.ConnectionFactory.class);
377381
Map<Object, org.springframework.amqp.rabbit.connection.ConnectionFactory> factories =
378382
new HashMap<Object, org.springframework.amqp.rabbit.connection.ConnectionFactory>(2);
379383
factories.put("foo", connectionFactory1);
380384
factories.put("bar", connectionFactory2);
385+
factories.put("baz", connectionFactory3);
386+
factories.put("qux", connectionFactory4);
381387

382388

383389
AbstractRoutingConnectionFactory connectionFactory = new SimpleRoutingConnectionFactory();
384390
connectionFactory.setTargetConnectionFactories(factories);
385391

386392
final RabbitTemplate template = new RabbitTemplate(connectionFactory);
387-
Expression expression = new SpelExpressionParser()
393+
Expression sendExpression = new SpelExpressionParser()
388394
.parseExpression("T(org.springframework.amqp.rabbit.core.RabbitTemplateTests)" +
389395
".LOOKUP_KEY_COUNT.getAndIncrement() % 2 == 0 ? 'foo' : 'bar'");
390-
template.setSendConnectionFactorySelectorExpression(expression);
391-
template.setReceiveConnectionFactorySelectorExpression(expression);
396+
template.setSendConnectionFactorySelectorExpression(sendExpression);
397+
Expression receiveExpression = new SpelExpressionParser()
398+
.parseExpression("T(org.springframework.amqp.rabbit.core.RabbitTemplateTests)" +
399+
".LOOKUP_KEY_COUNT.getAndIncrement() % 2 == 0 ? 'baz' : 'qux'");
400+
template.setReceiveConnectionFactorySelectorExpression(receiveExpression);
392401

393402
for (int i = 0; i < 3; i++) {
394403
try {
@@ -411,8 +420,10 @@ public void testRoutingConnectionFactory() throws Exception {
411420
}
412421
}
413422

414-
Mockito.verify(connectionFactory1, Mockito.times(5)).createConnection();
415-
Mockito.verify(connectionFactory2, Mockito.times(4)).createConnection();
423+
Mockito.verify(connectionFactory1, times(2)).createConnection();
424+
Mockito.verify(connectionFactory2, times(1)).createConnection();
425+
Mockito.verify(connectionFactory3, times(3)).createConnection();
426+
Mockito.verify(connectionFactory4, times(3)).createConnection();
416427
}
417428

418429
@Test

0 commit comments

Comments
 (0)