GH-1560: Caching CF toString() Improvements

Resolves https://github.com/spring-projects/spring-amqp/issues/1560

Use address resolution hierarchy to determine destination host(s).
This commit is contained in:
Gary Russell
2023-02-07 13:02:37 -05:00
committed by GitHub
parent 931896d4f0
commit 2f48c79acc
4 changed files with 49 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -303,6 +303,7 @@ public abstract class AbstractConnectionFactory implements ConnectionFactory, Di
}
@Override
@Nullable
public String getHost() {
return this.rabbitConnectionFactory.getHost();
}
@@ -354,6 +355,11 @@ public abstract class AbstractConnectionFactory implements ConnectionFactory, Di
this.addresses = null;
}
@Nullable
protected List<Address> getAddresses() throws IOException {
return this.addressResolver != null ? this.addressResolver.getAddresses() : this.addresses;
}
/**
* A composite connection listener to be used by subclasses when creating and closing connections.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.AlreadyClosedException;
import com.rabbitmq.client.BlockedListener;
import com.rabbitmq.client.Channel;
@@ -1003,8 +1004,21 @@ public class CachingConnectionFactory extends AbstractConnectionFactory
@Override
public String toString() {
return "CachingConnectionFactory [channelCacheSize=" + this.channelCacheSize + ", host=" + getHost()
+ ", port=" + getPort() + ", active=" + this.active
String host = getHost();
int port = getPort();
List<Address> addresses = null;
try {
addresses = getAddresses();
}
catch (IOException ex) {
host = "AddressResolver threw exception: " + ex.getMessage();
}
return "CachingConnectionFactory [channelCacheSize=" + this.channelCacheSize
+ (addresses != null
? ", addresses=" + addresses
: (host != null ? ", host=" + host : "")
+ (port > 0 ? ", port=" + port : ""))
+ ", active=" + this.active
+ " " + super.toString() + "]";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ public interface ConnectionFactory {
Connection createConnection() throws AmqpException;
@Nullable
String getHost();
int getPort();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -104,6 +104,28 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest
return ccf;
}
@Test
void stringRepresentation() {
CachingConnectionFactory ccf = new CachingConnectionFactory("someHost", 1234);
assertThat(ccf.toString()).contains(", host=someHost, port=1234")
.doesNotContain("addresses");
ccf.setAddresses("h1:1234,h2:1235");
assertThat(ccf.toString()).contains(", addresses=[h1:1234, h2:1235]")
.doesNotContain("host")
.doesNotContain("port");
ccf.setAddressResolver(() -> List.of(new Address("h3", 1236), new Address("h4", 1237)));
assertThat(ccf.toString()).contains(", addresses=[h3:1236, h4:1237]")
.doesNotContain("host")
.doesNotContain("port");
ccf.setAddressResolver(() -> {
throw new IOException("test");
});
ccf.setPort(0);
assertThat(ccf.toString()).contains(", host=AddressResolver threw exception: test")
.doesNotContain("addresses")
.doesNotContain("port");
}
@Test
public void testWithConnectionFactoryDefaults() throws Exception {
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);