Tutorial :httpclient timeouts? can's seem to get them to stick



Question:

Here is the code I'm using to create my multithreaded httpclient object. I'm trying to have a 4 second timeout across the board so if nothing happens for 4 seconds to drop the requests. I'm seeing really long execution times on some requests like in the 300 second range. I can't seem to figure out why that's happening. Any ideas?

HttpParams httpParams = new BasicHttpParams();      HttpConnectionParams.setConnectionTimeout(httpParams, 4000);      HttpConnectionParams.setSoTimeout(httpParams, 4000);        // set request params        httpParams.setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);      httpParams.setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");      httpParams.setParameter("http.protocol.wait-for-continue", 4000);        SchemeRegistry schemeRegistry = new SchemeRegistry();      schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));        ClientConnectionManager cm = new ThreadSafeClientConnManager(httpParams,schemeRegistry);      HttpClient httpClient = new DefaultHttpClient(cm, httpParams);  


Solution:1

All the timeout values you set only try to limit the time for a specific operation, not total time of the request. In certain circumstances, the timeout doesn't work at all. For example, the connection timeout has no effect if the IP is blocked by firewall.

The only solution I found is to start my request in a different thread and time it out myself. You can use ThreadPool and FutureTask for this. See my answer to this question,

java native Process timeout


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »