hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Cannot shut down the connection manager
Date Fri, 26 Oct 2007 10:29:14 GMT

On Fri, 2007-10-26 at 11:26 +0200, bischoff@informatik.rwth-aachen.de
wrote:
> Dear httpclient-team,
> 
> I want to implement a http client which is "robust" with respect to dumb
> servers, i.e. I want it to automatically shut down if a server does not
> accept data of a post request for say 10 seconds. I've tried to set the
> socket timeout in the http client, but that does not work - the client
> just hangs when it writes to the output stream. Then I've created a timer
> task to call the shutdown() method of the connection manager, but that
> doesn't work also - again the client hangs. Finally, I edited the
> closeSocketAndStreams() method in org.apache.commons.httpclient.HttpConnection
> to first close the socket and then close the streams (currently it's the
> other way round). If I do this, the shutdown() method works, but of course,
> I'd rather not edit httpclient's source code. Below I've attached the source
> I've used for testing this (tried Java 1.4 and 1.5).
> 
> Actually, I'm not even sure whether my approach to solve the above problem
> is sensible (e.g. is the connection manager thread safe?), so any help would
> be highly appreciated.
> 

Essentially you want to enforce an upload timeout, don't you? If so,
socket timeout will be of little use as it only affects read operations.
Create a timer task and have it call HttpMethod#abort() when the timeout
condition is met.

Hope this helps

Oleg


> Thank you,
> Stephan
> 
> 
> // a very dumb server
> public class HttpServer {
>   public static void main(String [] args) {
>     ServerSocket serverSocket = null;
>     try {
>       serverSocket = new ServerSocket(9999);
>       while (true)
>         serverSocket.accept();
>     }
>     catch (IOException ex) {
>       System.out.println("caught " + ex);
>     }
>   }
> }
> 
> // a request entity that writes data as long as it can
> public class MyRequestEntity implements RequestEntity
> {
>   public void writeRequest(OutputStream out) throws IOException {
>     for (int i = 0; true; ++i) {
>       System.out.println(i);
>       out.write(128); // this is the place where the client hangs
>     }
>   }
>   (...)
>   public long getContentLength() {
>     return 10000000;
>   }
> }
> 
> public class ShutDownTask extends TimerTask
> {
>   private SimpleHttpConnectionManager connectionManager;
>   public ShutDownTask(SimpleHttpConnectionManager connectionManager) {
>     this.connectionManager = connectionManager;
>   }
> 
>   public void run() {
>     connectionManager.shutdown();
>   }
> }
> 
> public class MyHttpClient {
>   public static void main(String [] args)
>   {
>     PostMethod method = new PostMethod("http://localhost:9999");
>     method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(10000));
// setting this timeout does not help
> 
>     method.setRequestEntity(new MyRequestEntity());
>     SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(true);
> 
>     Timer timer = new Timer();
>     timer.schedule(new ShutDownTask(connectionManager), 10000);
>     try {
>       int statusCode = new HttpClient(connectionManager).executeMethod(method);
>       System.out.println("got status code " + statusCode);
>     }
>     catch (IOException ex) {
>       System.out.println("caught " + ex);
>     }
>     finally {
>       method.releaseConnection();
>     }
>   }
> }
> 
> ---------------------------------------------------------------------
> 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


Mime
View raw message