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 bi...@informatik.rwth-aachen.de on 2007/10/26 11:26:55 UTC

Cannot shut down the connection manager

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.

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


Re: Cannot shut down the connection manager

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2007-10-26 at 14:31 +0200, bischoff@informatik.rwth-aachen.de
wrote:
> Oleg,
> 
> yes, I want to implement an upload timeout. I immediately tried
> your suggestion, but it also hangs. Actually, both a call to the
> ConnectionManager's shutdown() method and the call to HttpMethod's
> abort() method eventually will invoke closeSocketAndStreams() of
> HttpConnection. This method in turn invokes close() on the socket's
> output stream but will block infinitely as the socket is still
> open, so the socket should probably be closed first.

I see the problem. 

>  Do you know
> any other workarounds?
> 

No, actually, I do not. HttpClient 4.0 provides a means to forcibly
shutdown a connection among many improvements. I do not know of a way to
introduce similar functionality to HttpClient 3.x without forking its
code.

Sorry.

Oleg


> Thanks,
> Stephan
> 
> ----- Original Message -----
> From: Oleg Kalnichevski <ol...@apache.org>
> Date: Friday, October 26, 2007 12:30 pm
> Subject: Re: Cannot shut down the connection manager
> To: HttpClient User Discussion <ht...@jakarta.apache.org>
> 
> > 
> > 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
> 
> ---------------------------------------------------------------------
> 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: Cannot shut down the connection manager

Posted by bi...@informatik.rwth-aachen.de.
Oleg,

yes, I want to implement an upload timeout. I immediately tried
your suggestion, but it also hangs. Actually, both a call to the
ConnectionManager's shutdown() method and the call to HttpMethod's
abort() method eventually will invoke closeSocketAndStreams() of
HttpConnection. This method in turn invokes close() on the socket's
output stream but will block infinitely as the socket is still
open, so the socket should probably be closed first. Do you know
any other workarounds?

Thanks,
Stephan

----- Original Message -----
From: Oleg Kalnichevski <ol...@apache.org>
Date: Friday, October 26, 2007 12:30 pm
Subject: Re: Cannot shut down the connection manager
To: HttpClient User Discussion <ht...@jakarta.apache.org>

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

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


Re: Cannot shut down the connection manager

Posted by Oleg Kalnichevski <ol...@apache.org>.
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