Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 94233aa

Browse files
rwandercWander Costa
authored and
Wander Costa
committed
GH-27 Fixed compatibility with SpringBoot 2.2.13
1 parent e144484 commit 94233aa

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3939
<maven.compiler.source>1.8</maven.compiler.source>
4040
<maven.compiler.target>1.8</maven.compiler.target>
41-
<springboot.version>2.2.0.RELEASE</springboot.version>
41+
<springboot.version>2.2.13.RELEASE</springboot.version>
4242
</properties>
4343

4444
<scm>

spring-multirabbit-examples/spring-multirabbit-example-java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.2.0.RELEASE</version>
9+
<version>2.2.13.RELEASE</version>
1010
<relativePath />
1111
</parent>
1212

spring-multirabbit-examples/spring-multirabbit-example-kotlin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.2.0.RELEASE</version>
9+
<version>2.2.13.RELEASE</version>
1010
<relativePath />
1111
</parent>
1212

spring-multirabbit-examples/spring-multirabbit-extension-example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.2.0.RELEASE</version>
9+
<version>2.2.13.RELEASE</version>
1010
<relativePath />
1111
</parent>
1212

spring-multirabbit-lib-integration/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.2.0.RELEASE</version>
9+
<version>2.2.13.RELEASE</version>
1010
<relativePath />
1111
</parent>
1212

spring-multirabbit-lib/src/main/java/org/springframework/boot/autoconfigure/amqp/MultiRabbitAutoConfiguration.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,20 @@ public MultiRabbitConnectionFactoryWrapper externalEmptyWrapper() {
132132
public ConnectionFactory routingConnectionFactory(
133133
final RabbitProperties rabbitProperties,
134134
final MultiRabbitProperties multiRabbitProperties,
135-
final MultiRabbitConnectionFactoryWrapper externalWrapper) {
135+
final MultiRabbitConnectionFactoryWrapper externalWrapper) throws Exception {
136136
final MultiRabbitConnectionFactoryWrapper internalWrapper
137137
= instantiateConnectionFactories(rabbitProperties, multiRabbitProperties);
138138
final MultiRabbitConnectionFactoryWrapper aggregatedWrapper
139139
= aggregateConnectionFactoryWrappers(internalWrapper, externalWrapper);
140140

141+
if (aggregatedWrapper.getDefaultConnectionFactory() == null) {
142+
throw new IllegalArgumentException("A default ConnectionFactory must be provided.");
143+
}
144+
141145
aggregatedWrapper.getContainerFactories().forEach(this::registerContainerFactoryBean);
142146
aggregatedWrapper.getRabbitAdmins().forEach(this::registerRabbitAdminBean);
143147

144-
SimpleRoutingConnectionFactory connectionFactory = new SimpleRoutingConnectionFactory();
148+
final SimpleRoutingConnectionFactory connectionFactory = new SimpleRoutingConnectionFactory();
145149
connectionFactory.setTargetConnectionFactories(aggregatedWrapper.getConnectionFactories());
146150
connectionFactory.setDefaultTargetConnectionFactory(aggregatedWrapper.getDefaultConnectionFactory());
147151
return connectionFactory;
@@ -183,19 +187,20 @@ private void copyConnectionSets(
183187
*/
184188
private MultiRabbitConnectionFactoryWrapper instantiateConnectionFactories(
185189
final RabbitProperties rabbitProperties,
186-
final MultiRabbitProperties multiRabbitProperties) {
190+
final MultiRabbitProperties multiRabbitProperties) throws Exception {
187191
final MultiRabbitConnectionFactoryWrapper wrapper = new MultiRabbitConnectionFactoryWrapper();
188192

189193
final Map<String, RabbitProperties> propertiesMap = multiRabbitProperties != null
190194
? multiRabbitProperties.getConnections()
191195
: Collections.emptyMap();
192196

193-
propertiesMap.forEach((key, value) -> {
194-
CachingConnectionFactory connectionFactory = instantiateConnectionFactory(value);
195-
SimpleRabbitListenerContainerFactory containerFactory = newContainerFactory(connectionFactory);
196-
RabbitAdmin rabbitAdmin = newRabbitAdmin(connectionFactory);
197-
wrapper.addConnectionFactory(key, connectionFactory, containerFactory, rabbitAdmin);
198-
});
197+
for (Map.Entry<String, RabbitProperties> entry : propertiesMap.entrySet()) {
198+
final CachingConnectionFactory connectionFactory
199+
= springFactoryCreator.rabbitConnectionFactory(entry.getValue(), connectionNameStrategy);
200+
final SimpleRabbitListenerContainerFactory containerFactory = newContainerFactory(connectionFactory);
201+
final RabbitAdmin rabbitAdmin = newRabbitAdmin(connectionFactory);
202+
wrapper.addConnectionFactory(entry.getKey(), connectionFactory, containerFactory, rabbitAdmin);
203+
}
199204

200205
final String defaultConnectionFactoryKey = multiRabbitProperties != null
201206
? multiRabbitProperties.getDefaultConnection()
@@ -211,7 +216,7 @@ private MultiRabbitConnectionFactoryWrapper instantiateConnectionFactories(
211216

212217
final ConnectionFactory defaultConnectionFactory = StringUtils.hasText(defaultConnectionFactoryKey)
213218
? wrapper.getConnectionFactories().get(defaultConnectionFactoryKey)
214-
: instantiateConnectionFactory(rabbitProperties);
219+
: springFactoryCreator.rabbitConnectionFactory(rabbitProperties, connectionNameStrategy);
215220
wrapper.setDefaultConnectionFactory(defaultConnectionFactory);
216221

217222
return wrapper;
@@ -236,19 +241,6 @@ private RabbitAdmin newRabbitAdmin(final ConnectionFactory connectionFactory) {
236241
return rabbitAdmin;
237242
}
238243

239-
/**
240-
* Method to call default Spring factory creator and suppress a possible Exception into a RuntimeException. The
241-
* suppression of the Exception will not affect the flow, since Spring will still stop its initialization in the
242-
* event of any RuntimeException.
243-
*/
244-
private CachingConnectionFactory instantiateConnectionFactory(final RabbitProperties rabbitProperties) {
245-
try {
246-
return springFactoryCreator.rabbitConnectionFactory(rabbitProperties, connectionNameStrategy);
247-
} catch (Exception ex) {
248-
throw new RuntimeException(ex);
249-
}
250-
}
251-
252244
/**
253245
* Registers the ContainerFactory bean.
254246
*/

spring-multirabbit-lib/src/test/java/org/springframework/boot/autoconfigure/amqp/MultiRabbitConnectionFactoryCreatorTest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.mockito.ArgumentMatchers.any;
2626
import static org.mockito.ArgumentMatchers.eq;
27+
import static org.mockito.Mockito.never;
2728
import static org.mockito.Mockito.verify;
2829
import static org.mockito.Mockito.verifyNoMoreInteractions;
2930
import static org.mockito.Mockito.when;
@@ -89,13 +90,16 @@ public void shouldInstantiateExternalEmptyWrapper() {
8990
}
9091

9192
@Test
92-
public void shouldInstantiateRoutingConnectionFactory() {
93+
public void shouldInstantiateRoutingConnectionFactory() throws Exception {
94+
when(springFactoryCreator.rabbitConnectionFactory(rabbitProperties, connectionNameStrategy))
95+
.thenReturn(new CachingConnectionFactory());
96+
9397
assertTrue(creator().routingConnectionFactory(rabbitProperties, multiRabbitProperties, wrapper)
9498
instanceof RoutingConnectionFactory);
9599
}
96100

97101
@Test
98-
public void shouldInstantiateRoutingConnectionFactoryWithDefaultAndMultipleConnections() {
102+
public void shouldInstantiateRoutingConnectionFactoryWithDefaultAndMultipleConnections() throws Exception {
99103
when(wrapper.getDefaultConnectionFactory()).thenReturn(connectionFactory0);
100104
when(wrapper.getConnectionFactories()).thenReturn(singletonMap(DUMMY_KEY, connectionFactory1));
101105
when(wrapper.getContainerFactories()).thenReturn(singletonMap(DUMMY_KEY, containerFactory));
@@ -112,7 +116,7 @@ public void shouldInstantiateRoutingConnectionFactoryWithDefaultAndMultipleConne
112116
}
113117

114118
@Test
115-
public void shouldInstantiateRoutingConnectionFactoryWithOnlyDefaultConnectionFactory() {
119+
public void shouldInstantiateRoutingConnectionFactoryWithOnlyDefaultConnectionFactory() throws Exception {
116120
when(wrapper.getDefaultConnectionFactory()).thenReturn(connectionFactory0);
117121

118122
ConnectionFactory routingConnectionFactory = creator().routingConnectionFactory(rabbitProperties,
@@ -123,10 +127,12 @@ public void shouldInstantiateRoutingConnectionFactoryWithOnlyDefaultConnectionFa
123127
}
124128

125129
@Test
126-
public void shouldInstantiateRoutingConnectionFactoryWithOnlyMultipleConnectionFactories() {
130+
public void shouldInstantiateRoutingConnectionFactoryWithOnlyMultipleConnectionFactories() throws Exception {
127131
when(wrapper.getConnectionFactories()).thenReturn(singletonMap(DUMMY_KEY, connectionFactory0));
128132
when(wrapper.getContainerFactories()).thenReturn(singletonMap(DUMMY_KEY, containerFactory));
129133
when(wrapper.getRabbitAdmins()).thenReturn(singletonMap(DUMMY_KEY, rabbitAdmin));
134+
when(springFactoryCreator.rabbitConnectionFactory(rabbitProperties, connectionNameStrategy))
135+
.thenReturn(new CachingConnectionFactory());
130136

131137
ConnectionFactory routingConnectionFactory = creator().routingConnectionFactory(rabbitProperties,
132138
multiRabbitProperties, wrapper);
@@ -139,7 +145,7 @@ public void shouldInstantiateRoutingConnectionFactoryWithOnlyMultipleConnectionF
139145
}
140146

141147
@Test
142-
public void shouldReachDefaultConnectionFactoryWhenNotBound() {
148+
public void shouldReachDefaultConnectionFactoryWhenNotBound() throws Exception {
143149
when(wrapper.getDefaultConnectionFactory()).thenReturn(connectionFactory0);
144150
when(wrapper.getConnectionFactories()).thenReturn(singletonMap(DUMMY_KEY, connectionFactory1));
145151
when(wrapper.getContainerFactories()).thenReturn(singletonMap(DUMMY_KEY, containerFactory));
@@ -148,11 +154,11 @@ public void shouldReachDefaultConnectionFactoryWhenNotBound() {
148154
creator().routingConnectionFactory(rabbitProperties, multiRabbitProperties, wrapper).getVirtualHost();
149155

150156
verify(connectionFactory0).getVirtualHost();
151-
verifyNoMoreInteractions(connectionFactory1);
157+
verify(connectionFactory1, never()).getVirtualHost();
152158
}
153159

154160
@Test
155-
public void shouldBindAndReachMultiConnectionFactory() {
161+
public void shouldBindAndReachMultiConnectionFactory() throws Exception {
156162
when(wrapper.getDefaultConnectionFactory()).thenReturn(connectionFactory0);
157163
when(wrapper.getConnectionFactories()).thenReturn(singletonMap(DUMMY_KEY, connectionFactory1));
158164
when(wrapper.getContainerFactories()).thenReturn(singletonMap(DUMMY_KEY, containerFactory));
@@ -165,12 +171,15 @@ public void shouldBindAndReachMultiConnectionFactory() {
165171
routingConnectionFactory.getVirtualHost();
166172
SimpleResourceHolder.unbind(routingConnectionFactory);
167173

168-
verifyNoMoreInteractions(connectionFactory0);
174+
verify(connectionFactory0, never()).getVirtualHost();
169175
verify(connectionFactory1).getVirtualHost();
170176
}
171177

172178
@Test
173-
public void shouldInstantiateMultiRabbitConnectionFactoryWrapperWithDefaultConnection() {
179+
public void shouldInstantiateMultiRabbitConnectionFactoryWrapperWithDefaultConnection() throws Exception {
180+
when(springFactoryCreator.rabbitConnectionFactory(rabbitProperties, connectionNameStrategy))
181+
.thenReturn(new CachingConnectionFactory());
182+
174183
assertNotNull(creator().routingConnectionFactory(rabbitProperties, null, wrapper));
175184
}
176185

@@ -181,6 +190,7 @@ public void shouldInstantiateMultiRabbitConnectionFactoryWrapperWithMultipleConn
181190

182191
MultiRabbitProperties multiRabbitProperties = new MultiRabbitProperties();
183192
multiRabbitProperties.getConnections().put(DUMMY_KEY, secondaryRabbitProperties);
193+
multiRabbitProperties.setDefaultConnection(DUMMY_KEY);
184194

185195
creator().routingConnectionFactory(null, multiRabbitProperties, wrapper);
186196

@@ -204,7 +214,7 @@ public void shouldInstantiateMultiRabbitConnectionFactoryWrapperWithDefaultAndMu
204214

205215
@Test
206216
public void shouldEncapsulateExceptionWhenFailingToCreateBean() throws Exception {
207-
exception.expect(RuntimeException.class);
217+
exception.expect(Exception.class);
208218
exception.expectMessage("mocked-exception");
209219

210220
when(springFactoryCreator.rabbitConnectionFactory(any(RabbitProperties.class), eq(connectionNameStrategy)))

0 commit comments

Comments
 (0)