From 1a4a06acfa3adbbf8971a008e619972e75017360 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 2 Sep 2011 16:17:53 +0000 Subject: 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 --- .../sca/host/http/client/HttpClientFactory.java | 38 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'sca-java-2.x/trunk/modules') 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; } } -- cgit v1.2.3