01: /* Copyright 2006 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.services;
07:
08: import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
09: import org.apache.commons.httpclient.HttpClient;
10: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
11: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
12: import org.apache.commons.httpclient.params.HttpMethodParams;
13: import org.apache.commons.logging.Log;
14: import org.apache.commons.logging.LogFactory;
15: import org.jasig.portal.properties.PropertiesManager;
16:
17: /**
18: * Centralized access to the HttpClient connection manager
19: * @author George Lindholm
20: * @since uPortal 2.5
21: * @version $Revision: 36736 $
22: */
23: public class HttpClientManager {
24: private static final Log log = LogFactory
25: .getLog(HttpClientManager.class);
26:
27: private static final int POOL_SIZE = PropertiesManager
28: .getPropertyAsInt(
29: "org.jasig.portal.services.HttpClientManager.poolSize",
30: 20);
31: private static final int HOST_CONNECTIONS = PropertiesManager
32: .getPropertyAsInt(
33: "org.jasig.portal.services.HttpClientManager.hostConnections",
34: 2);
35: private static final int DEFAULT_CONNECTION_TIMEOUT = PropertiesManager
36: .getPropertyAsInt(
37: "org.jasig.portal.services.HttpClientManager.connectionTimeout",
38: 5000); // five seconds
39:
40: private static final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
41: static {
42: final HttpConnectionManagerParams pars = connectionManager
43: .getParams();
44: pars.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
45: pars.setParameter(HttpMethodParams.RETRY_HANDLER,
46: new DefaultHttpMethodRetryHandler(3, false));
47: pars.setMaxTotalConnections(POOL_SIZE);
48: pars.setDefaultMaxConnectionsPerHost(HOST_CONNECTIONS);
49: }
50:
51: public static HttpClient getNewHTTPClient() {
52: return new HttpClient(connectionManager);
53: }
54:
55: public static int getActiveConnections() {
56: return connectionManager.getConnectionsInPool();
57: }
58:
59: public static int getMaxConnections() {
60: return POOL_SIZE;
61: }
62: }
|