The IOException occurred while using HttpClient connect Elasticsearch

I used the HttpClient to connect Elasticsearch. The code likes these,

@Test
public void queryIndices() throws IOException {

    // new a get request
    HttpGet request = new HttpGet("http://localhost:9200/_cat/indices?pretty");

    // config the timeout
    RequestConfig config = RequestConfig.custom()
            .setSocketTimeout(2000)
            .setConnectTimeout(2000)
            .setConnectionRequestTimeout(2000)
            .build();
    request.setConfig(config);

    // construct a httpClient and execute the get request
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(request);

    // get the response message
    HttpEntity entity = response.getEntity();
    String responseText = EntityUtils.toString(entity, StandardCharsets.UTF_8);
    System.out.println(responseText);

    // close the response
    EntityUtils.consume(entity);
    response.close();
    
}

While this Unit Test has ran and terminated, the Elasticsearch thrown such a excpetion,

[2016-07-19 14:30:48,329][WARN ][http.netty ] [Sin] Caught exception while handling client http traffic, closing connection [id: 0x9940a6e4, /127.0.0.1:65380 => /127.0.0.1:9200]
java.io.IOException: An existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:192)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:64)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Howerver, the result of Unit Test is green, I want to know will it has effect on the Elasticsearch and can it be fixed.

Request your kind help!
Thanks!

Close the httpClient. I'd use try-with-resources for everything with the word "closeable" in the name.

2 seconds is a bit low for aggregations, btw. If you are going to set a timeone on the connection like that you should also set it on the Elasticsearch request so Elasticsearch can make an effort to timeout the request internally.

1 Like

Thanks for your help!
It is my mistake that forget close the httpClient, and in the actual environment, it will run until the app closed.
I will configure the Elasticsearch's timeout and adjust the timeout to the "aggregation operation" if i use.