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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to