Skip to content

Commit 2f48c79

Browse files
authored
GH-1560: Caching CF toString() Improvements
Resolves #1560 Use address resolution hierarchy to determine destination host(s).
1 parent 931896d commit 2f48c79

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -303,6 +303,7 @@ public void setUri(String uri) {
303303
}
304304

305305
@Override
306+
@Nullable
306307
public String getHost() {
307308
return this.rabbitConnectionFactory.getHost();
308309
}
@@ -354,6 +355,11 @@ public synchronized void setAddresses(String addresses) {
354355
this.addresses = null;
355356
}
356357

358+
@Nullable
359+
protected List<Address> getAddresses() throws IOException {
360+
return this.addressResolver != null ? this.addressResolver.getAddresses() : this.addresses;
361+
}
362+
357363
/**
358364
* A composite connection listener to be used by subclasses when creating and closing connections.
359365
*

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -58,6 +58,7 @@
5858
import org.springframework.util.ObjectUtils;
5959
import org.springframework.util.StringUtils;
6060

61+
import com.rabbitmq.client.Address;
6162
import com.rabbitmq.client.AlreadyClosedException;
6263
import com.rabbitmq.client.BlockedListener;
6364
import com.rabbitmq.client.Channel;
@@ -1003,8 +1004,21 @@ protected ExecutorService getChannelsExecutor() {
10031004

10041005
@Override
10051006
public String toString() {
1006-
return "CachingConnectionFactory [channelCacheSize=" + this.channelCacheSize + ", host=" + getHost()
1007-
+ ", port=" + getPort() + ", active=" + this.active
1007+
String host = getHost();
1008+
int port = getPort();
1009+
List<Address> addresses = null;
1010+
try {
1011+
addresses = getAddresses();
1012+
}
1013+
catch (IOException ex) {
1014+
host = "AddressResolver threw exception: " + ex.getMessage();
1015+
}
1016+
return "CachingConnectionFactory [channelCacheSize=" + this.channelCacheSize
1017+
+ (addresses != null
1018+
? ", addresses=" + addresses
1019+
: (host != null ? ", host=" + host : "")
1020+
+ (port > 0 ? ", port=" + port : ""))
1021+
+ ", active=" + this.active
10081022
+ " " + super.toString() + "]";
10091023
}
10101024

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -33,6 +33,7 @@ public interface ConnectionFactory {
3333

3434
Connection createConnection() throws AmqpException;
3535

36+
@Nullable
3637
String getHost();
3738

3839
int getPort();

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -104,6 +104,28 @@ protected AbstractConnectionFactory createConnectionFactory(ConnectionFactory co
104104
return ccf;
105105
}
106106

107+
@Test
108+
void stringRepresentation() {
109+
CachingConnectionFactory ccf = new CachingConnectionFactory("someHost", 1234);
110+
assertThat(ccf.toString()).contains(", host=someHost, port=1234")
111+
.doesNotContain("addresses");
112+
ccf.setAddresses("h1:1234,h2:1235");
113+
assertThat(ccf.toString()).contains(", addresses=[h1:1234, h2:1235]")
114+
.doesNotContain("host")
115+
.doesNotContain("port");
116+
ccf.setAddressResolver(() -> List.of(new Address("h3", 1236), new Address("h4", 1237)));
117+
assertThat(ccf.toString()).contains(", addresses=[h3:1236, h4:1237]")
118+
.doesNotContain("host")
119+
.doesNotContain("port");
120+
ccf.setAddressResolver(() -> {
121+
throw new IOException("test");
122+
});
123+
ccf.setPort(0);
124+
assertThat(ccf.toString()).contains(", host=AddressResolver threw exception: test")
125+
.doesNotContain("addresses")
126+
.doesNotContain("port");
127+
}
128+
107129
@Test
108130
public void testWithConnectionFactoryDefaults() throws Exception {
109131
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);

0 commit comments

Comments
 (0)