Hi Oleg,
Thanks for the confirmation.

I am using HttpClient in my application which is a kind of reverse proxy
inside my tomcat web application where I use HttpClient to perform all
HTTP GET and POST with the backend web servers and then send back the
obtained contents from the HttpResponse in the actual
HttpServletResponse instance to the client (browser). For this I have to
read from the HttpResponse entity inputstream and write the contents
into the OutputStream of the HttpServletResponse.
Any suggestions here on the ClientAbortException resulting from the
SocketException ?

public void sendStreamToBrowser(HttpServletResponse response) throws
IOException {
        InputStream streamFromResponse = null;
        // Get hold of the response entity
         HttpEntity entity = this.httpResponse.getEntity();
         if (entity != null) {
                 streamFromResponse = entity.getContent();
         }
        OutputStream responseStream = response.getOutputStream();
        
        if (streamFromResponse!= null) {
            byte[] buffer = new byte[1024];
            int read = streamFromResponse.read(buffer);
            while (read > 0) {
                responseStream.write(buffer, 0, read);
                read = streamFromResponse.read(buffer);
            } 
                 
        }
        responseStream.flush();
        responseStream.close();
          entity.consumeContent(); //called to properly close and
release all underlying resources
    }

I am getting a Socket exception on the line: responseStream.flush();

The stack trace:

ClientAbortException:  java.net.SocketException: Connection reset by
peer: socket write error
        at
org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:327
)
        at
org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:293)
        at
org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStrea
m.java:97)
        at
sample.test.MyResponseHandler.sendStreamToBrowser(MyResponseHandler.java
:123)

Thanks,
Brijesh


-----Original Message-----
From: Oleg Kalnichevski [mailto:[email protected]] 
Sent: Wednesday, April 01, 2009 4:32 PM
To: HttpClient User Discussion
Subject: Re: How to release connection with HttpClient 4.0 api ?

On Wed, 2009-04-01 at 15:24 +0530, Brijesh Deo wrote:
> Hi,
> 
>  
> 
> I am trying to port my earlier code which used HttpClient 3.1 to now
use
> HttpClient 4.0. I am trying to find out a corresponding method with
4.0
> to gracefully release a connection as it was being done with the 3.1
> api.
> 
>  
> 
> With HC 3.1:
> 
> GetMethod method = new GetMethod("/someURL");
> 
> //execute the method using the HttpClient instance
> 
> //and then finally release the connection as shown below.
> 
> method.releaseConnection();
> 
>  
> 
> 
> 
> With HC 4.0:
> 
> HttpGet httpGet = new HttpGet("/someURL");
> 
> //execute the method using the HttpClient instance
> 
> HttpResponse response = httpClient.execute(httpget);
> 
>  
> 
> How to release the connection gracefully?
> 
> I don't want to call the abort() method on the httpGet instance.
> 
>  
> 
> Is it sufficient to call the consumeContect() to release a connection
> gracefully?
> 
> HttpEntity entity = response.getEntity();
> 
> If (entity != null) {
> 
>             entity.consumeContent();
> 
> }
> 
>  
> 
> 
> 
> Please let me know if there is a better and more efficient way to do
it.

This is indeed the recommended way. For details, see the 'Ensuring
release of low level resources' section of the tutorial

http://wiki.apache.org/HttpComponents/HttpClientTutorial


> I have also tried to get the underlying inputstream from the response
> entity and calling a close() on that but it's resulting in a
> SocketException from other parts in my code.
> 

This should also work. What kind of SocketException are you getting? 

Oleg




>  
> 
> Thanks,
> 
> Brijesh
> 
>  
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to