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 "Cordes, Hans-Dieter" <hd...@emea.att.com> on 2005/05/20 16:01:39 UTC

HttpClient: Release all resources due to a Thread.interrupt

Hello,

I need some assistance with the following problem:

I have a thread X that will be allowed to live only for a specified amount of time. In that thread I have an HttpClient object that I use to send a POST to another server machine. Now I want to make sure that when my thread X is interrupted, because its time-out has expired, I am able to release all resources of the HttpClient object. What do I have to do?

I would think that basically I should simply close the corresponding socket via the HttpConnection object that HttpClient uses. But I have not found a way to get this HttpConnection object given that I have references to HttpClient and PostMethod. (Perhaps I should first close/release the request/response I/O channels, but when the socket goes down, this should be done automatically. And perhaps I could also use the HttpClient.setTimeout method?)

Many thanks in advance for your help.

Regards,
	Hans-Dieter Cordes

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


Re: HttpClient: Release all resources due to a Thread.interrupt

Posted by Oleg Kalnichevski <ol...@apache.org>.
Hans-Dieter,

A trivial socket read timeout will do. Just make sure you always release
connection back to the manager regardless whether the request succeeds
or fails

GetMethod httpget = new GetMethod("/whatever");
try {
  httpclient.executeMethod(httpget);
  // do stuff
} finally {
  httpget.releaseConnection();
}

Alternatively, if you are using HttpClient 3.0, you may want to call
HttpMethod#abort to close the underlying socket

Oleg

On Fri, 2005-05-20 at 16:01 +0200, Cordes, Hans-Dieter wrote:
> Hello,
> 
> I need some assistance with the following problem:
> 
> I have a thread X that will be allowed to live only for a specified amount of time. In that thread I have an HttpClient object that I use to send a POST to another server machine. Now I want to make sure that when my thread X is interrupted, because its time-out has expired, I am able to release all resources of the HttpClient object. What do I have to do?
> 
> I would think that basically I should simply close the corresponding socket via the HttpConnection object that HttpClient uses. But I have not found a way to get this HttpConnection object given that I have references to HttpClient and PostMethod. (Perhaps I should first close/release the request/response I/O channels, but when the socket goes down, this should be done automatically. And perhaps I could also use the HttpClient.setTimeout method?)
> 
> Many thanks in advance for your help.
> 
> Regards,
> 	Hans-Dieter Cordes
> 
> ---------------------------------------------------------------------
> 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