Skip to content

Commit

Permalink
GH-1560: Caching CF toString() Improvements
Browse files Browse the repository at this point in the history
Resolves #1560

Use address resolution hierarchy to determine destination host(s).
  • Loading branch information
garyrussell authored Feb 7, 2023
1 parent 931896d commit 2f48c79
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -303,6 +303,7 @@ public void setUri(String uri) {
}

@Override
@Nullable
public String getHost() {
return this.rabbitConnectionFactory.getHost();
}
Expand Down Expand Up @@ -354,6 +355,11 @@ public synchronized void setAddresses(String addresses) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -58,6 +58,7 @@
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;
Expand Down Expand Up @@ -1003,8 +1004,21 @@ protected ExecutorService getChannelsExecutor() {

@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() + "]";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,6 +33,7 @@ public interface ConnectionFactory {

Connection createConnection() throws AmqpException;

@Nullable
String getHost();

int getPort();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -104,6 +104,28 @@ protected AbstractConnectionFactory createConnectionFactory(ConnectionFactory co
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);
Expand Down

0 comments on commit 2f48c79

Please sign in to comment.