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: Socket Exception
Date Thu, 10 Feb 2005 21:11:28 GMT
Prasad, 

See my comment in-line

On Wed, 2005-02-09 at 17:54 -0700, Prasad Sethumadhavan wrote:
> Thanks Oleg,
> The reason I was confused was because the SocketException was due to
> "recv failed".


I am not 100% sure but the exact type of IO exception seems to be web
server/app server/platform/JRE dependent. I may be wrong, though.


>  So I thought if the server had closed the connection then
> I would have gotten "connection reset by peer" or some sort of a send
> error(unless the server closed the connection after it received the
> request). I am not sure how/if the request is sent correctly.
> 
> I have added both your suggestions. 
> 
>         /** Set a custom http method retry handler */
>  
> httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
>                 new CustomMethodRetryHandler());
>         
>         /** Close idle connection after a certain period of inactivity
> */
>         IdleConnectionTimeoutThread idleConnectionTimeoutThread = new 
>             IdleConnectionTimeoutThread();
>         idleConnectionTimeoutThread.addConnectionManager(manager);
>  
> idleConnectionTimeoutThread.setTimeoutInterval(IDLE_CONNECTION_CLEANUP_I
> NTERVAL);
>  
> idleConnectionTimeoutThread.setConnectionTimeout(IDLE_CONNECTION_TIMEOUT
> );
>  
> idleConnectionTimeoutThread.setName("IdleConnectionTimeoutThread");
>         idleConnectionTimeoutThread.start();
> 
>     protected static class CustomMethodRetryHandler extends
> DefaultHttpMethodRetryHandler {
>         protected CustomMethodRetryHandler() {
>             super(MAX_METHOD_TRIES, true);
>         }        
>         public boolean retryMethod(
>             final HttpMethod method, 
>             final IOException exception, 
>             int executionCount) {
>             
>             /** 
>              * If stale connection is used, the request fails with 
>              * the SocketException. Retry in such cases
>              */
>             boolean retry = super.retryMethod(method, exception,
> executionCount) || 
>                 (exception instanceof SocketException);
>             return retry;
>         }
>     }
> 
> I my custom handler I am using requestSentRetryEnabled=true as I am
> getting "recv failed". Is this bad or not a good idea? 


Again, it all depends if HTTP methods that your application executes are
safe to retry. Consider a situation where the same purchase order or
account transfer gets executed multiple times because of a retry on the
transport layer. As long as the HTTP method does not change the data on
the server side it is always safe to retry, otherwise it depends upon
the exact design of the server application.


> Please let me know if the above code is the correct way to fix the issue
> or if there are better ways to do it.

Seem okay to me


> 
> Is there a plan to implement the RFC2965 for cookie handling in the near
> future? If so what would be the time frame?
> 

Yes. See <http://issues.apache.org/bugzilla/show_bug.cgi?id=10813>. If
you contribute it it will be faster ;-) Otherwise, I can't say. It is
not the top priority issue as far as I am concerned.

Hope this helps

Oleg


> Thanks for helping out
> Prasad
> 
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org] 
> Sent: Wednesday, February 09, 2005 3:51 PM
> To: HttpClient User Discussion
> Subject: RE: Socket Exception
> 
> Prasad,
> 
> First of all, by HTTP spec both the client and the server may drop
> connection at any point of time without giving a prior warning of any
> sort. 
> 
> HttpClient usually keeps the connection open indefinitely if the
> connection is reusable (HTTP/1.1 connection or HTTP/1.0 connection
> explicitly marked as reusable with 'connection:keep-alive'). The server,
> however, may choose to close this connection after a certain period of
> inactivity, thus rendering the connection 'stale' on the client side.
> HttpClient in its turn has no way of knowing if the connection is still
> valid (we do perform a so called 'stale' connection check but it cannot
> be 100% reliable). Next time the stale connection is used, the request
> fails with the SocketException
> 
> You have several options
> 
> (1) do not keep persistent connections open indefinitely on the client
> side and close them after a certain period of inactivity
> (2) retry the failed request PROVIDED the request is idempotent.
> 
> Oleg
> 
> On Wed, 2005-02-09 at 15:29 -0700, Prasad Sethumadhavan wrote:
> > Hi Oleg,
> > Thanks for the reply. I have added a custom retry handler now. However
> 
> > my main concern is why this exception is happening in the first place.
> > If I use the http client without any major break (even heavy load 
> > tests with 100-200 clients works great). However if I use the 
> > HttpClient sparsely like once every 3 minutes or so why am I getting 
> > this exception. Is this happening because the http client is resuing 
> > connections and the connection was closed by the server? If so then 
> > why am I getting a "recv failed" consistently?
> > I guess this use case of using the http client sparsely is very
> common.
> > So is it a configuration problem on my part? Please help me out.
> > 
> > Thanks
> > Prasad
> > 
> > -----Original Message-----
> > From: Oleg Kalnichevski [mailto:olegk@apache.org]
> > Sent: Wednesday, February 09, 2005 2:25 PM
> > To: HttpClient User Discussion
> > Subject: Re: Socket Exception
> > 
> > Prasad,
> > 
> > See <http://jakarta.apache.org/commons/httpclient/3.0/exception-
> > handling.html#HTTP%20transport%20safety>
> > 
> > Oleg
> > 
> > On Tue, 2005-02-08 at 16:43 -0700, Prasad Sethumadhavan wrote:
> > > Hi,
> > > I am getting a SocketException if I use a HttpClient instance after 
> > > a gap of 3 minutes.
> > > 
> > > //Initialization
> > > 
> > >         MultiThreadedHttpConnectionManager manager = 
> > >             new MultiThreadedHttpConnectionManager();
> > >         manager.getParams().setStaleCheckingEnabled(true);
> > >         manager.getParams().setDefaultMaxConnectionsPerHost(
> > >                 config.getMaxHttpConnections());
> > >         manager.getParams().setMaxTotalConnections(
> > >                 config.getMaxHttpConnections());
> > >         manager.getParams().setConnectionTimeout(
> > >                 config.getDefaultConnectionTimeout());
> > >         
> > >         Httpclient httpclient = new HttpClient(manager);
> > >         httpclient.getParams().setCookiePolicy(
> > >                 CookiePolicy.BROWSER_COMPATIBILITY);
> > > 
> > > I create a new method to a URL and execute it using 
> > > httpClient.executeMethod(). I then sleep for 180 seconds and then 
> > > access the same URL using a newly created method and I am getting 
> > > the following exception. I can reproduce this consistently.
> > > 
> > > Please let me know if there is some property that I need to set to 
> > > avoid this problem. Also, in my application, requests can come at 
> > > any time and hence I should be able to use the httpclient after any
> > interval.
> > > 
> > > Thanks in advance
> > > Prasad
> > > 
> > > //Stack Trace
> > > 
> > > java.net.SocketException: Software caused connection abort: recv
> > failed
> > > 	at java.net.SocketInputStream.socketRead0(Native Method)
> > > 	at java.net.SocketInputStream.read(SocketInputStream.java:129)
> > > 	at
> > > java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
> > > 	at
> > > java.io.BufferedInputStream.read(BufferedInputStream.java:235)
> > > 	at
> > >
> > org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:7
> > 6)
> > > 	at
> > >
> org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:104)
> > > 	at
> > > org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection
> > > .j
> > > av
> > > a:1112)
> > > 	at
> > > org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$Htt
> > > pC
> > > on
> > >
> nectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1379)
> > > 	at
> > > org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMeth
> > > od
> > > Ba
> > > se.java:1825)
> > > 	at
> > > org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethod
> > > Ba
> > > se
> > > .java:1588)
> > > 	at
> > > org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.
> > > ja
> > > va
> > > :999)
> > > 	at
> > > org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Ht
> > > tp
> > > Me
> > > thodDirector.java:382)
> > > 	at
> > > org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpM
> > > et
> > > ho
> > > dDirector.java:168)
> > > 	at
> > > org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.ja
> > > va
> > > :3
> > > 93)
> > > 
> > > --------------------------------------------------------------------
> > > - 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
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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