java.net.UnknownHostException only when using CloseableHttpAsyncClient

1.7k views Asked by At

I have built a REST application using Spring Boot, that in turn invokes another 3rd party REST API. I am trying to use Apache's HTTPComponents CloseableHttpAsyncClient with Spring's AsyncRestTemplate. This is how I set up AsyncRestTemplate in the Spring Boot application:

@Bean
public AsyncRestTemplate asyncRestTemplate() throws IOReactorException {
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
    PoolingNHttpClientConnectionManager cm =
        new PoolingNHttpClientConnectionManager(ioReactor);
    CloseableHttpAsyncClient client =
        HttpAsyncClients.custom().setConnectionManager(cm).build();
    client.start();
    return new AsyncRestTemplate(
        new HttpComponentsAsyncClientHttpRequestFactory(client));
}

The code that invokes the 3rd party REST API endpoint is as follows:

ListenableFuture<ResponseEntity<String>> restFuture =
    asyncRestTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);

When this line executes, I get the following exception:

java.net.UnknownHostException: <<api.3rdpartyservices.com>>
    at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[na:1.8.0_112]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_112]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_112]
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:609) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:580) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:427) ~[httpcore-nio-4.4.5.jar:4.4.5]
    at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:276) ~[httpcore-nio-4.4.5.jar:4.4.5]
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:266) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:363) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:125) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:75) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:108) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.springframework.http.client.HttpComponentsAsyncClientHttpRequest.executeInternal(HttpComponentsAsyncClientHttpRequest.java:96) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]

But if I replaced the initialization of AsyncRestTemplate as follows, without changing anything else:

@Bean
public AsyncRestTemplate asyncRestTemplate() throws IOReactorException {
return new AsyncRestTemplate(
    new HttpComponentsAsyncClientHttpRequestFactory());
}

the code executes just fine. This is on a Mac (OS X El Capitan). Spring Boot version is 1.4.2, Java 8 and HttpAsyncClient 4.1.2. How can I resolve this?

Note: I replaced the actual hostname with a dummy hostname (api.3rdpartyservices.com) since I am unable to divulge that information. Thanks to @JimGarrison for the comment, so I can clarify this for everyone.

0

There are 0 answers