Problem closing resources using Apache HttpClient.

R

R

I'm using Apache HTTPClient to connect to a data source.

I set connection timeout and socket timeout in the constructor of the
class like the following:
httpClient.getHttpConnectionManager().getParams().setSoTimeout(1000*60*2);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000*60*2);

In one of the method in the class, I catch SocketTimeoutException and
close resources on the finally clause;
try
{
GetMethod getMethodObject = new GetMethod(DATA_URL);
int statusCode = httpClient.executeMethod(getMethodObject);
responseBody = getMethodObject.getResponseBodyAsStream();
reader = new BufferedReader(new InputStreamReader(responseBody));

String line= "";
while( (line = reader.readLine()) != null )
{
....
}
}
catch(SocketTimeoutException ex)
{

}
catch(...)
....
finally
{
if( reader != null )
{
try { reader.close(); }
catch (Exception ex) { logger.warning("Could not close
BufferedReader:"+ex);}
}

if( responseBody != null )
{
try { responseBody.close(); }
catch (Exception ex) { logger.warning("Could not close InputStream from
the server."); }
}

if( getMethodObject != null )
{
try { getMethodObject.releaseConnection(); }
catch (Exception ex) { logger.warning("Could not release connection.");
}
}
}

Once in a while when I get SocketTimeoutException, I got the message
could not close BufferedReader.
These are the messages that I got:
"Could not Close BufferedReader: java.net.SocketException:Connection
Reset."
"Could not Close BufferedReader: java.net.SocketTimeoutException:Read
Timeout."

In other programs that uses the same program flow, I got this message:
"Could not Close BufferedReader: CRLF expected at the end of chunk"

And once again, this is not happening all the time but only once in a
while. Could somebody please advise?

Thanks in advance for all the help.
I really appreciate it.
 
R

R

My current suspicion is that when something wrong with the connection,
the BufferedReader won't be closed properly.
But why is it only happening randomly?

Is there any way to get around it

This seems to happen with my program that uses Apache HttpClient to
connect to the data source.

Any help would be greatly appreciated.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top