Skip to content

Commit ea01566

Browse files
authored
GH-1443: Pull CCF.resetConnection() to CF
Resolves #1443 **Cherry-pick to `2.4.x`**
1 parent 38da8c7 commit ea01566

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractRoutingConnectionFactory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.ConcurrentHashMap;
2323

2424
import org.springframework.amqp.AmqpException;
25+
import org.springframework.beans.factory.DisposableBean;
2526
import org.springframework.beans.factory.InitializingBean;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
@@ -38,7 +39,7 @@
3839
* @since 1.3
3940
*/
4041
public abstract class AbstractRoutingConnectionFactory implements ConnectionFactory, RoutingConnectionFactory,
41-
InitializingBean {
42+
InitializingBean, DisposableBean {
4243

4344
private final Map<Object, ConnectionFactory> targetConnectionFactories =
4445
new ConcurrentHashMap<Object, ConnectionFactory>();
@@ -260,4 +261,15 @@ protected ConnectionFactory removeTargetConnectionFactory(Object key) {
260261
@Nullable
261262
protected abstract Object determineCurrentLookupKey();
262263

264+
@Override
265+
public void destroy() {
266+
resetConnection();
267+
}
268+
269+
@Override
270+
public void resetConnection() {
271+
this.targetConnectionFactories.values().forEach(factory -> factory.resetConnection());
272+
this.defaultTargetConnectionFactory.resetConnection();
273+
}
274+
263275
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java

Lines changed: 2 additions & 1 deletion
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.
@@ -882,6 +882,7 @@ public final void destroy() {
882882
* used to force a reconnect to the primary broker after failing over to a secondary
883883
* broker.
884884
*/
885+
@Override
885886
public void resetConnection() {
886887
synchronized (this.connectionMonitor) {
887888
if (this.connection.target != null) {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConnectionFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -83,4 +83,12 @@ default boolean isPublisherReturns() {
8383
return false;
8484
}
8585

86+
/**
87+
* Close any connection(s) that might be cached by this factory. This does not prevent
88+
* new connections from being opened.
89+
* @since 2.4.4
90+
*/
91+
default void resetConnection() {
92+
}
93+
8694
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/LocalizedQueueConnectionFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-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.
@@ -350,7 +350,7 @@ protected ConnectionFactory createConnectionFactory(String address, String node)
350350
}
351351

352352
@Override
353-
public void destroy() {
353+
public void resetConnection() {
354354
Exception lastException = null;
355355
for (ConnectionFactory connectionFactory : this.nodeFactories.values()) {
356356
if (connectionFactory instanceof DisposableBean) {
@@ -367,4 +367,9 @@ public void destroy() {
367367
}
368368
}
369369

370+
@Override
371+
public void destroy() {
372+
resetConnection();
373+
}
374+
370375
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/PooledChannelConnectionFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public synchronized Connection createConnection() throws AmqpException {
149149
* used to force a reconnect to the primary broker after failing over to a secondary
150150
* broker.
151151
*/
152+
@Override
152153
public void resetConnection() {
153154
destroy();
154155
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ThreadChannelConnectionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-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.
@@ -140,6 +140,7 @@ public void closeThreadChannel() {
140140
* used to force a reconnect to the primary broker after failing over to a secondary
141141
* broker.
142142
*/
143+
@Override
143144
public void resetConnection() {
144145
destroy();
145146
}

0 commit comments

Comments
 (0)