Hi All,
This is my first post on this list. Although I've been looking
through the archives, I could not find an answer to my specific
problem, so here goes...
I've attempted this with both the stable (3.0.1) and current
(3.1beta1) version of the httpclient binaries.
I have been trying to determine how to correctly set the timeout
parameters in order to limit the connection time and force the code
below to complete within a maximum amount of time.
The host that I'm connecting to may be intermittently available. The
httpclient code works in situations where the host responds in a
reasonable amount of time, but if the host is unavailable the thread
hangs, potentially forever, or at least a long enough for this to
cause problems when attempting to scale the code to use hundreds of
connections in several minutes.
my code:
final int SOCKET_TIMEOUT = 1000;
final int CONNECTION_TIMEOUT = 1000;
long start = System.currentTimeMillis();
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod( UNREACHABLE_HOST );
httpget.getParams().setSoTimeout( SOCKET_TIMEOUT );
httpget.getParams().setParameter( "http.connection.timeout",
CONNECTION_TIMEOUT );
httpget.getParams().setParameter( "http.method.retry-handler", new
DefaultHttpMethodRetryHandler( 0, false ) );
try {
httpclient.executeMethod( httpget );
InputStream instream = httpget.getResponseBodyAsStream();
} catch( IOException ioe) {
} finally {
httpget.releaseConnection();
}
long time = System.currentTimeMillis() - start;
System.out.println( "Took " + time + "ms" );
I presumed that a socket timeout would ensure that the call to
httpclient.execute() will return or throw an exception after
sock_timeout ms. However, my tests of this code show that varying
sock_timeout has no effect on the time spend in
httpclient.executeMethod( httpget ) which is consistently 75s (when
the host is unresponsive, of course). I tried varying conn_timeout as
well, with similar results.
I have also attempted to run similar code in its own thread and using
a separate timer, call httpget.abort(); httpget.releaseConnection().
This doesn't change the behavour of the program: the thread still
hangs for 75s.
In fact, the only progress I've had on this has been to set the
http.method.retry-handler parameter so that the connection is not
retried after it hits the first 75s timeout. It occurs to me that
this 75s limit is really the timeout I want to adjust.
Am I incorrectly interpreting the use of the http.socket.timeout
parameter? Is there an alternate way to guarantee that the thread
never spends more than a certain amount of time in the
httpclient.executeMethod() function? Is there a way to adjust the 75s
timeout that I'm hitting here? Or is there a way to force the
connection to release?
Thanks in advance,
Graeme