Apache HttpClient CoreConnectionPNames.CONNECTION_TIMEOUT什么都不做? [英] Apache HttpClient CoreConnectionPNames.CONNECTION_TIMEOUT does nothing?

查看:665
本文介绍了Apache HttpClient CoreConnectionPNames.CONNECTION_TIMEOUT什么都不做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从HttpClient获得奇怪的行为,参数CoreConnectionPNames.CONNECTION_TIMEOUT设置为1.
我希望HttpGet请求失败,抛出连接超时异常但它们成功了。这似乎是不合理的,因为它实际上意味着TCP握手在不到1毫秒内完成。

I get strange behavior from HttpClient with parameter CoreConnectionPNames.CONNECTION_TIMEOUT set to 1. I would expect that HttpGet request would fail, throwing connection timeout exception and yet they are successful. This seems irrational as it actually means that TCP handshake finished in less then 1 millisecond.

我正在使用的httpclient版本可以在这个pom.xml中看到

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>

以下是代码:

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());
    }
}

这怎么可能?
谢谢。

How is this possible? Thank you.

推荐答案

我所使用过的所有JVM中的超时有效粒度大约是15- 30毫秒。即使超时设置为1个套接字I / O,连接请求通常也会成功,如果它们的时间少于15-30毫秒。

The effective granularity of time-outs in all JVMs I have worked with is approximately 15-30ms. Even if the timeout is set to 1 socket I/O and connect requests often succeed if they take less than 15-30ms.

这篇关于Apache HttpClient CoreConnectionPNames.CONNECTION_TIMEOUT什么都不做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆