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.
Output
200 OK http://www.google.com/
200 OK http://www.google.com/
[snip]
5 requests using Async Client in: 2308 ms
The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.
timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.
request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server.
The Request Timeout property specifies the number of seconds the server waits between accepting a connection to a client and receiving information from it. The default setting is 30 seconds.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With