hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Checkoway <dchecko...@gmail.com>
Subject Timeouts not being obeyed
Date Fri, 13 Apr 2012 01:40:19 GMT
Hello,

I'm a long-time very happy user of HttpClient.  I have bumped into what I
believe is a bug...but it's possible I'm just abusing the API and missing
something simple.  :-)

I use ThreadSafeClientConnManager and a single shared instance of
DefaultHttpClient to manage a persistent HTTP connection pool.  It works
exactly as I need it to -- except it's not obeying timeouts.

What I need to do is set an arbitrary timeout for each individual request.
That is, I can't just set one timeout up front when I create the
HttpClient.  The timeout I need to use varies from request to request.

The way I'm *trying* to convey the timeout is by setting attributes on the
HttpContext as shown in the code below...but what I'm experiencing is that
my timeouts are being ignored.

I believe the root of the problem is that AbstractHttpClient is using its
determineParams() method, but that's completely disregarding the params
which may have been set on the context.

Can you let me know if (a) this is a bug and I'm doing things correctly on
my end, or (b) there's another proper way to convey desired SO_TIMEOUT
and/or connect timeout on a per-request basis?

Thanks!

public class HttpTimeoutExample {
    private ThreadSafeClientConnManager connMgr;
    private HttpClient httpClient;

    public HttpTimeoutBugExample(int connTtlMs, int maxTotal, int
defaultMaxPerRoute) {
        this.defaultConnectionTimeout = defaultConnectionTimeout;
        this.defaultSoTimeout = defaultSoTimeout;

        connMgr = new
ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(),
connTtlMs, TimeUnit.MILLISECONDS);
        connMgr.setMaxTotal(maxTotal);
        connMgr.setDefaultMaxPerRoute(defaultMaxPerRoute);

        httpClient = new DefaultHttpClient(connMgr);
    }

    public void doGetWithTimeout(String someUrl, boolean handleRedirects,
int timeLimitMs) {
        HttpUriRequest httpRequest = new HttpGet(someUrl);

        BasicHttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientPNames.HANDLE_REDIRECTS,
Boolean.valueOf(handleRedirects));

        // Set timeouts...these are being ignored!
        httpContext.setAttribute(CoreConnectionPNames.CONNECTION_TIMEOUT,
Integer.valueOf(timeLimitMs));
        httpContext.setAttribute(CoreConnectionPNames.SO_TIMEOUT,
Integer.valueOf(timeLimitMs));

        HttpResponse httpResponse = httpClient.execute(httpRequest,
httpContext);
        HttpEntity httpEntity = httpResponse.getEntity();
        try {
            // read the response entity
        } finally {
            EntityUtils.consume(httpEntity);
        }
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message