12

I want to run thread safe, asynchronous HTTP requests using HTTPClient. I noticed that it does not respect my CONNECTION_TIMEOUT argument.

The code is ColdFusion / Java hybrid.

client = loader.create("org.apache.http.impl.nio.client.DefaultHttpAsyncClient").init();
CoreConnectionPNames = loader.create("org.apache.http.params.CoreConnectionPNames");

client.getParams()
      .setIntParameter(JavaCast("string", CoreConnectionPNames.SO_TIMEOUT), 10)
      .setIntParameter(JavaCast("string", CoreConnectionPNames.CONNECTION_TIMEOUT), 10);

client.start();

request = loader.create("org.apache.http.client.methods.HttpGet").init("http://www.google.com");
future = client.execute(request, javacast("null", ""));

try {
   response = future.get();
}
catch(e any) {}

client.getConnectionManager().shutdown();

  Regardless of what I supply for CONNECTION_TIMEOUT, the requests always return 200 OK. Check the output below.

  1. How do I set an effective connection timeout?
  2. Does CONNECTION_TIMEOUT do anything?

Output

200 OK http://www.google.com/

200 OK http://www.google.com/

[snip]

5 requests using Async Client in: 2308 ms
Mohamad
  • 34,731
  • 32
  • 140
  • 219

2 Answers2

3

The documentation for apache's HttpClient is kind of spotty. Try this in your setup (it worked for me with version 4):

HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);

... set more parameters here if you want to ...

SchemeRegistry schemeRegistry = new SchemeRegistry();

.. do whatever you ant with the scheme registry here ...

ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

client = new DefaultHttpClient(connectionManager, params);
Seth
  • 5,596
  • 8
  • 42
  • 56
  • Thanks. I'm trying your code out, but it takes extra work to translate it to CF. So bare with me. In the first two linse `HTTPConnectionParams` refers to an object that I need to create. Porbably `org.apache.http.params.*` but where does the `params` argument you supply to `setConnectionTimeout` come from? I can't throw it in there, otherwise it would throw a `params is undefined` error... sorry if this sounds obtuse, but working with java/CF hybrid code is tedious. Also, is the SchemeRegistry object necessary, can I just leave it out? – Mohamad Oct 15 '11 at 15:59
  • HttpParams params = new BasicHttpParams(); There's some more information here: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html and the javadoc is here: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/HttpParams.html – Seth Oct 15 '11 at 16:25
2

You have to define a HttpParams object using the framework's class methods.

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, 2000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        HttpClient client = DefaultHttpClient(ccm, params);
Ryhan
  • 1,815
  • 1
  • 18
  • 22