8000 Merge remote-tracking branch 'origin/master' into rabbitmq-java-clien… · CassOnMars/rabbitmq-java-client@97d21bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 97d21bf

Browse files
committed
Merge remote-tracking branch 'origin/master' into rabbitmq-java-client-174
Integrate AddressResolver implementation for DNS record IPs.
2 parents 0c0c4f4 + 2c6b1cd commit 97d21bf

File tree

5 files changed

+151
-24
lines changed

5 files changed

+151
-24
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.rabbitmq.client;
22

3+
import java.io.IOException;
34
import java.util.List;
45

56
/**
67
* Strategy interface to get the potential servers to connect to.
78
*/
89
public interface AddressResolver {
910

10-
List<Address> getAddresses();
11+
/**
12+
* Get the potential {@link Address}es to connect to.
13+
* @return candidate {@link Address}es
14+
* @throws IOException if it encounters a problem
15+
*/
16+
List<Address> getAddresses() throws IOException;
1117

1218
}

src/main/java/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,21 @@
1515

1616
package com.rabbitmq.client;
1717

18-
import java.io.IOException;
19-
import java.security.KeyManagementException;
20-
import java.security.NoSuchAlgorithmException;
21-
import java.util.Collections;
22-
import java.util.Map;
23-
import java.util.HashMap;
24-
import java.util.concurrent.*;
25-
import java.util.List;
26-
import java.util.Arrays;
27-
28-
import java.net.URI;
29-
import java.net.URISyntaxException;
30-
import java.net.URLDecoder;
18+
import com.rabbitmq.client.impl.*;
19+
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
3120

3221
import javax.net.SocketFactory;
33-
import javax.net.ssl.SSLSocketFactory;
3422
import javax.net.ssl.SSLContext;
23+
import javax.net.ssl.SSLSocketFactory;
3524
import javax.net.ssl.TrustManager;
36-
37-
import com.rabbitmq.client.impl.AMQConnection;
38-
import com.rabbitmq.client.impl.ConnectionParams;
39-
import com.rabbitmq.client.impl.DefaultExceptionHandler;
40-
import com.rabbitmq.client.impl.FrameHandler;
41-
import com.rabbitmq.client.impl.FrameHandlerFactory;
42-
import com.rabbitmq.client.impl.recovery.AutorecoveringConnection;
25+
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
28+
import java.net.URLDecoder;
29+
import java.security.KeyManagementException;
30+
import java.security.NoSuchAlgorithmException;
31+
import java.util.*;
32+
import java.util.concurrent.*;
4333

4434
/**
4535
* Convenience "factory" class to facilitate opening a {@link Connection} to an AMQP broker.
@@ -836,7 +826,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres
836826
*/
837827
public Connection newConnection(ExecutorService executor, List<Address> addrs, String clientProvidedName)
838828
throws IOException, TimeoutException {
839-
return newConnection(executor, new ListAddressResolver(addrs), clientProvidedName);
829+
return newConnection(executor, createAddressResolver(addrs), clientProvidedName);
840830
}
841831

842832
/**
@@ -976,6 +966,14 @@ public Connection newConnection(ExecutorService executor, String connectionName)
976966
return newConnection(executor, Collections.singletonList(new Address(getHost(), getPort())), connectionName);
977967
}
978968

969+
protected AddressResolver createAddressResolver(List<Address> addresses) {
970+
if(addresses.size() == 1) {
971+
return new DnsRecordIpAddressResolver(addresses.get(0), isSSL());
972+
} else {
973+
return new ListAddressResolver(addresses);
974+
}
975+
}
976+
979977
@Override public ConnectionFactory clone(){
980978
try {
981979
return (ConnectionFactory)super.clone();
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.rabbitmq.client;
2+
3+
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.UnknownHostException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* {@link AddressResolver} that resolves DNS record IPs.
11+
* Uses {@link InetAddress} internally.
12+
* The first returned address is used when automatic recovery is NOT enabled
13+
* at the {@link ConnectionFactory} level.
14+
* When automatic recovery is enabled, a random address will be picked up
15+
* from the returned list of {@link Address}es.
16+
*/
17+
public class DnsRecordIpAddressResolver implements AddressResolver {
18+
19+
private final Address address;
20+
21+
private final boolean ssl;
22+
23+
public DnsRecordIpAddressResolver(String hostname, int port, boolean ssl) {
24+
this(new Address(hostname, port), ssl);
25+
}
26+
27+
public DnsRecordIpAddressResolver(String hostname, int port) {
28+
this(new Address(hostname, port), false);
29+
}
30+
31+
public DnsRecordIpAddressResolver() {
32+
this("localhost");
33+
}
34+
35+
public DnsRecordIpAddressResolver(String hostname) {
36+
this(new Address(hostname), false);
37+
}
38+
39+
public DnsRecordIpAddressResolver(Address address) {
40+
this(address, false);
41+
}
42+
43+
public DnsRecordIpAddressResolver(Address address, boolean ssl) {
44+
this.address = address;
45+
this.ssl = ssl;
46+
}
47+
48+
/**
49+
* Get the IP addresses from a DNS query
50+
* @return candidate {@link Address}es
51+
* @throws IOException if DNS resolution fails
52+
*/
53+
@Override
54+
public List<Address> getAddresses() throws IOException {
55+
String hostName = address.getHost();
56+
int portNumber = ConnectionFactory.portOrDefault(address.getPort(), ssl);
57+
InetAddress[] inetAddresses;
58+
try {
59+
inetAddresses = resolveIpAddresses(hostName);
60+
} catch (UnknownHostException e) {
61+
throw new IOException("Could not resolve IP addresses for host "+hostName, e);
62+
}
63+
List<Address> addresses = new ArrayList<Address>();
64+
for (InetAddress inetAddress : inetAddresses) {
65+
addresses.add(new Address(inetAddress.getHostAddress(), portNumber));
66+
}
67+
return addresses;
68+
}
69+
70+
protected InetAddress[] resolveIpAddresses(String hostName) throws UnknownHostException {
71+
return InetAddress.getAllByName(hostName);
72+
}
73+
74+
}

src/test/java/com/rabbitmq/client/test/ClientTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
AMQBuilderApiTest.class,
4040
AmqpUriTest.class,
4141
JSONReadWriteTest.class,
42-
SharedThreadPoolTest.class
42+
SharedThreadPoolTest.class,
43+
DnsRecordIpAddressResolverTests.class
4344
})
4445
public class ClientTests extends AbstractRMQTestSuite {
4546

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.rabbitmq.client.test;
2+
3+
import com.rabbitmq.client.Connection;
4+
import com.rabbitmq.client.ConnectionFactory;
5+
import com.rabbitmq.client.DnsRecordIpAddressResolver;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.TimeoutException;
9+
10+
/**
11+
*
12+
*/
13+
public class DnsRecordIpAddressResolverTests extends BrokerTestCase {
14+
15+
public void testLocalhostResolution() throws IOException, TimeoutException {
16+
DnsRecordIpAddressResolver addressResolver = new DnsRecordIpAddressResolver("localhost");
17+
ConnectionFactory connectionFactory = newConnectionFactory();
18+
Connection connection = connectionFactory.newConnection(addressResolver);
19+
try {
20+
connection.createChannel();
21+
} finally {
22+
connection.abort();
23+
}
24+
}
25+
26+
public void testLoopbackInterfaceResolution() throws IOException, TimeoutException {
27+
DnsRecordIpAddressResolver addressResolver = new DnsRecordIpAddressResolver("127.0.0.1");
28+
ConnectionFactory connectionFactory = newConnectionFactory();
29+
Connection connection = connectionFactory.newConnection(addressResolver);
30+
try {
31+
connection.createChannel();
32+
} finally {
33+
connection.abort();
34+
}
35+
}
36+
37+
public void testResolutionFails() throws IOException, TimeoutException {
38+
DnsRecordIpAddressResolver addressResolver = new DnsRecordIpAddressResolver(
39+
"afancyandunlikelyhostname"
40+
);
41+
try {
42+
connectionFactory.newConnection(addressResolver);
43+
fail("The host resolution should have failed");
44+
} catch (IOException e) {
45+
// expected
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)
0