You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Graeme Rouse <gr...@arizonabay.com> on 2006/11/07 21:33:27 UTC

Recovering a connection from an unresponsive host

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


Re: Recovering a connection from an unresponsive host

Posted by Graeme Rouse <gr...@arizonabay.com>.
Olaf,

Thank you for the tip. Making this change simplifies my code and best  
of all, makes it work as I expect.

Thanks again for helping me out with this,

Graeme

On 8-Nov-06, at 3:59 AM, Olaf Sebelin wrote:

> Hello Graeme,
>
>
> Graeme Rouse <gr...@arizonabay.com> schrieb am 07.11.2006 um 15:33:
>
> [...]
>>
>> 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.
> [...]
>>
>> 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 );
>
> Try to set the timeout on the connection manager:
>
> httpclient.getHttpConnectionManager().getParams()
>   .setSoTimeout(CONNECTION_TIMEOUT);
>
> or
>
> MultiThreadedHttpConnectionManager connectionManager
>   = new MultiThreadedHttpConnectionManager();
>
> connectionManager.getParams().setConnectionTimeout 
> (CONNECTION_TIMEOUT);
>
> HttpClient httpclient = new HttpClient(connectionManager);
>
>
> Regards,
> Olaf
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user- 
> help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Recovering a connection from an unresponsive host

Posted by Olaf Sebelin <os...@bos-bremen.de>.
Hello Graeme,


Graeme Rouse <gr...@arizonabay.com> schrieb am 07.11.2006 um 15:33:

[...]
> 
> 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.
[...]
> 
> 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 );

Try to set the timeout on the connection manager:

httpclient.getHttpConnectionManager().getParams()
  .setSoTimeout(CONNECTION_TIMEOUT);

or 

MultiThreadedHttpConnectionManager connectionManager 
  = new MultiThreadedHttpConnectionManager();

connectionManager.getParams().setConnectionTimeout(CONNECTION_TIMEOUT);

HttpClient httpclient = new HttpClient(connectionManager);


Regards,
Olaf


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Recovering a connection from an unresponsive host

Posted by Roland Weber <ht...@dubioso.net>.
Hi Graeme,

> 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.

That's impossible. The timeout parameters apply to individual operations
performed as part of method execution. You cannot set an overall timeout.
You have to start an extra thread to achieve that effect.

> 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.

Sounds like a known issue which we can't solve in HttpClient 3.x:
https://issues.apache.org/jira/browse/HTTPCLIENT-475

> Am I incorrectly interpreting the use of the http.socket.timeout
> parameter?

As Olaf suggested, try setting the timeout on the connection manager
instead of the method.

> Is there an alternate way to guarantee that the thread never
> spends more than a certain amount of time in the
> httpclient.executeMethod() function?

There's one rather rude way to enforce the timeout. Instead of starting
a timer thread that tries to interrupt the main one, start a worker thread
that performs the actual operation. In the main thread, use Object.wait(ms)
to synchronize with the worker thread, try to terminate the worker if you
hit the timeout. Even if the worker thread is not correctly interrupted
due to the problem mentioned above, your main thread can continue. The
connection remains blocked until the worker thread hits it's internal
timeout, though.

hope that helps,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org