Peter,
I struggled with several timeouts for a little while, you can set a
connection timeout using:
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectTimeout);

A socket timeout using:
getMethod.getParams().setSoTimeout(socketTimeout); //getMethod is an
http method created earlier, but the example is pretty self explanatory

Another problem I had with timeouts was during a transfer when the
connection was established and the socket timeout did not apply due to
slow transfer or something, and that gets a little annoying if you want
to create something like a user timeout.  I had to create a scheduled
task, kind of like a unix sigalarm handler.

    private class TimeoutTimer extends TimerTask {
        private HttpMethod method=null;
        private int timeout=0;
        public void setTimeout( int i ) { timeout = i; }
        public int getTimeout() { return timeout; }
        public void setMethod( Object i ) { this.method = (HttpMethod)
i; }
        public HttpMethod getMethod() { return method; }

        public void run() {
            logger.error("Fetch URL Timed Out Due to user timeout of "+
(int) timeout/1000 +" seconds being reached");
            method.abort();
            method=null;
        }
    }

then schedule it (using java 1.5) and execute your method:
    TimeoutTimer timeoutTimer = new TimeoutTimer();
    Timer timer = new Timer();

    //then create your method as usual.....

    timeoutTimer.setMethod(getMethod);
    timeoutTimer.setTimeout(userTimeout);
    timer.schedule(timeoutTimer, userTimeout);
    httpClient.executeMethod(getMethod);
    timer.cancel();

I hope that helps.

Does anyone know if any future plans exist to extend the standard method
interface to include a user defined timeout to a method execution?  i'm
not entirely sure this feature would be in keeping with the HttpMethod
contract, not knowing how other people are using it.
Thanks, John


On Tue, 2006-02-14 at 16:51 +0100, Peter Wenngren wrote:

> Hello gurus!
> 
> I'm having problems with a very long Connection-timeout (about 3
> minutes!) time when trying to send a http-query to a non existing
> server. I've tried to set the setSoTimeout() method on the HttpClient
> instance and setConnectionManagerTimeout() on the
> MultiThreadedHttpConnectionManager used by the HttpClient. But, no
> success...
> 
> How do I set the maximum amount of time to wait for a connection to a
> non-existing host?
> 
> I'm using HttpClient v.3.0 on a Bea WLS 8.14 running on jrockit JVM.
> 
> Any suggestions on how I can abort the connection after a few
> seconds/failed attempts to produce connection?
> 
> Thanks!
> 
> // Peter Wenngren
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to