diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-02 16:17:53 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-02 16:17:53 +0000 |
commit | 1a4a06acfa3adbbf8971a008e619972e75017360 (patch) | |
tree | 81137defe91cf6bbf804df848f0bab9ebe91ece1 | |
parent | e6dfa9b94979c4c62542cb1680363e5c465ff46d (diff) |
Turn the http client factory into a utility
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1164607 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java index 4819107ffd..fe9fd49b56 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java +++ b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/client/HttpClientFactory.java @@ -23,6 +23,8 @@ import org.apache.http.HttpHost; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnManagerParams; +import org.apache.http.conn.params.ConnPerRoute; +import org.apache.http.conn.params.ConnPerRouteBean; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; @@ -34,18 +36,29 @@ import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HTTP; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; /** * @version $Rev$ $Date$ */ -public class HttpClientFactory { +public class HttpClientFactory implements LifeCycleListener { + + private HttpClient httpClient; + + public static HttpClientFactory getInstance(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + return utilities.getUtility(HttpClientFactory.class); + } public HttpClient createHttpClient() { HttpParams defaultParameters = new BasicHttpParams(); //defaultParameters.setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 10); - ConnManagerParams.setMaxTotalConnections(defaultParameters, 160); - // ConnManagerParams.setMaxConnectionsPerRoute(defaultParameters, ConnPerRoute); + ConnManagerParams.setMaxTotalConnections(defaultParameters, 1024); + ConnPerRoute connPerRoute = new ConnPerRouteBean(256); + ConnManagerParams.setMaxConnectionsPerRoute(defaultParameters, connPerRoute); HttpProtocolParams.setContentCharset(defaultParameters, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(defaultParameters, 60000); @@ -58,8 +71,25 @@ public class HttpClientFactory { ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(defaultParameters, supportedSchemes); + return new DefaultHttpClient(connectionManager, defaultParameters); + } + @Override + public void start() { + if (httpClient == null) { + httpClient = createHttpClient(); + } + } - return new DefaultHttpClient(connectionManager, defaultParameters); + @Override + public void stop() { + if (httpClient != null) { + httpClient.getConnectionManager().shutdown(); + httpClient = null; + } + } + + public HttpClient getHttpClient() { + return httpClient; } } |