Apache HttpClient CoreConnectionPNames.CONNECTION_TIMEOUT does nothing ?

Posted by Maxim Veksler on Stack Overflow See other posts from Stack Overflow or by Maxim Veksler
Published on 2010-06-08T19:54:49Z Indexed on 2010/06/08 20:42 UTC
Read the original article Hit count: 426

Hi,

I'm testing some result from HttpClient that looks irrational. It seems that setting CoreConnectionPNames.CONNECTION_TIMEOUT = 1 has no effect because send request to different host return successfully with connect timeout 1 which IMHO can't be the case (1ms to setup TCP handshake???)

Am I misunderstood something or is something very strange going on here?

The httpclient version I'm using as can be seen in this pom.xml is

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.0.1</version>
        <type>jar</type>
    </dependency>

Here is the code:

import java.io.IOException;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class TestNodeAliveness {
    private static Logger log = Logger.getLogger(TestNodeAliveness.class);

    public static boolean nodeBIT(String elasticIP) throws ClientProtocolException, IOException {
        try {
            HttpClient client = new DefaultHttpClient();

            // The time it takes to open TCP connection.
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1);

            // Timeout when server does not send data.
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

            // Some tuning that is not required for bit tests.
            client.getParams().setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
            client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, true);


            HttpUriRequest request = new HttpGet("http://" + elasticIP);
            HttpResponse response = client.execute(request);

            HttpEntity entity = response.getEntity();
            if(entity == null) { 
                return false;
            } else {
                System.out.println(EntityUtils.toString(entity));
            }

            // Close just in case.
            request.abort();

        } catch (Throwable e) {
            log.warn("BIT Test failed for " + elasticIP);
            e.printStackTrace();

            return false;
        }

        return true;
    }


    public static void main(String[] args) throws ClientProtocolException, IOException {
        nodeBIT("google.com?cant_cache_this=" + (new Random()).nextInt());
    }
}

Thank you.

© Stack Overflow or respective owner

Related posts about java

Related posts about network-programming