You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/07/01 12:54:45 UTC
svn commit: r552277 -
/jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
Author: olegk
Date: Sun Jul 1 03:54:44 2007
New Revision: 552277
URL: http://svn.apache.org/viewvc?view=rev&rev=552277
Log:
Improved exception handling based on Odi's suggestions
Modified:
jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java?view=diff&rev=552277&r1=552276&r2=552277
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java Sun Jul 1 03:54:44 2007
@@ -32,6 +32,7 @@
package org.apache.http.examples.client;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
@@ -73,27 +74,36 @@
// do something useful with the response
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
- System.out.println(reader.readLine());
- // In case of an IOException the connection will be released
- // back to the connection manager automatically
- // No need for ugly try-finally clause
-
- // If a runtime exception can be thrown in the process
- // of response processing make sure the HTTP request
- // gets aborted to ensure connection release.
try {
- // Do something that can throw a non-fatal
- // unchecked exception
+
+ System.out.println(reader.readLine());
+ // In case of an IOException the connection will be released
+ // back to the connection manager automatically
+
+ } catch (IOException ex) {
+
+ // In case of an IOException the connection will be released
+ // back to the connection manager automatically
+ throw ex;
+
} catch (RuntimeException ex) {
- // Abort HTTP request to ensure connection release
+
+ // In case of an unexpected exception you may want to abort
+ // the HTTP request in order to shut down the underlying
+ // connection and release it back to the connection manager.
httpget.abort();
+ throw ex;
+
+ } finally {
+
+ // Closing the input stream will trigger connection release
+ reader.close();
+ // Alternatively you may want to call this method when done
+ // with the response entity. It will also trigger connection
+ // release
+ entity.consumeContent();
+
}
-
- // When done with the response either close the stream
- reader.close();
- // Or call
- entity.consumeContent();
- // Either of these methods will cause connection release
}
}