From 2f48c79acca6f2e20b3fcea8dd586c23be818d08 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 7 Feb 2023 13:02:37 -0500 Subject: [PATCH] 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). --- .../connection/AbstractConnectionFactory.java | 8 ++++++- .../connection/CachingConnectionFactory.java | 20 +++++++++++++--- .../rabbit/connection/ConnectionFactory.java | 3 ++- .../CachingConnectionFactoryTests.java | 24 ++++++++++++++++++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java index 3bc3bf95..44a5bd27 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java @@ -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
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. * diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java index 1397d1d4..1502fca5 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java @@ -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
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() + "]"; } diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConnectionFactory.java index 2f4323b5..d435f9b3 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ConnectionFactory.java @@ -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(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java index 5126ea9a..5d8165ac 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java @@ -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);