下面列出了怎么用org.apache.commons.httpclient.HttpConnectionManager的API类实例代码及写法,或者点击链接到github查看源代码。
private void initConfigurationContext() throws Exception {
HttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
File configFile = new File(DEFAULT_AXIS2_XML);
if (!configFile.exists()) {
configurationContext = ConfigurationContextFactory.createDefaultConfigurationContext();
configurationContext.setProperty(HTTPConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST, MAX_CONNECTIONS_PER_HOST);
} else {
configurationContext = ConfigurationContextFactory.
createConfigurationContextFromFileSystem(DEFAULT_CLIENT_REPO, DEFAULT_AXIS2_XML);
}
configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
configurationContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);
Map<String, TransportOutDescription> transportsOut = configurationContext.getAxisConfiguration()
.getTransportsOut();
for (TransportOutDescription transportOutDescription : transportsOut.values()) {
if (Constants.TRANSPORT_HTTP.equals(transportOutDescription.getName()) || Constants.TRANSPORT_HTTPS
.equals(transportOutDescription.getName())) {
transportOutDescription.getSender().init(configurationContext, transportOutDescription);
}
}
}
/**
* Closes idle connections.
*/
public synchronized void run() {
while (!shutdown) {
Iterator iter = connectionManagers.iterator();
while (iter.hasNext()) {
HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next();
handleCloseIdleConnections(connectionManager);
}
try {
this.wait(timeoutInterval);
} catch (InterruptedException e) {
}
}
// clear out the connection managers now that we're shutdown
this.connectionManagers.clear();
}
private void initConfigurationContext() throws Exception {
HttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
File configFile = new File(DEFAULT_AXIS2_XML);
if (!configFile.exists()) {
configurationContext = ConfigurationContextFactory.createDefaultConfigurationContext();
configurationContext.setProperty(HTTPConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST, MAX_CONNECTIONS_PER_HOST);
} else {
configurationContext = ConfigurationContextFactory.
createConfigurationContextFromFileSystem(DEFAULT_CLIENT_REPO, DEFAULT_AXIS2_XML);
}
configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
configurationContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);
Map<String, TransportOutDescription> transportsOut =
configurationContext.getAxisConfiguration().getTransportsOut();
for (TransportOutDescription transportOutDescription : transportsOut.values()) {
if (Constants.TRANSPORT_HTTP.equals(transportOutDescription.getName()) ||
Constants.TRANSPORT_HTTPS.equals(transportOutDescription.getName())) {
transportOutDescription.getSender().init(configurationContext, transportOutDescription);
}
}
}
/**
* A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
*
* @param user
* can be NULL
* @param password
* can be NULL
* @return HttpClient
*/
public static HttpClient getHttpClientInstance(String user, String password) {
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionParams params = connectionManager.getParams();
// wait max 10 seconds to establish connection
params.setConnectionTimeout(10000);
// a read() call on the InputStream associated with this Socket
// will block for only this amount
params.setSoTimeout(10000);
HttpClient c = new HttpClient(connectionManager);
// use basic authentication if available
if (user != null && user.length() > 0) {
AuthScope authScope = new AuthScope(null, -1, null);
Credentials credentials = new UsernamePasswordCredentials(user, password);
c.getState().setCredentials(authScope, credentials);
}
return c;
}
/**
* A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
*
* @param user
* can be NULL
* @param password
* can be NULL
* @return HttpClient
*/
public static HttpClient getHttpClientInstance(String user, String password) {
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionParams params = connectionManager.getParams();
// wait max 10 seconds to establish connection
params.setConnectionTimeout(10000);
// a read() call on the InputStream associated with this Socket
// will block for only this amount
params.setSoTimeout(10000);
HttpClient c = new HttpClient(connectionManager);
// use basic authentication if available
if (user != null && user.length() > 0) {
AuthScope authScope = new AuthScope(null, -1, null);
Credentials credentials = new UsernamePasswordCredentials(user, password);
c.getState().setCredentials(authScope, credentials);
}
return c;
}
@Override
public void close()
{
if(httpClient != null)
{
HttpConnectionManager connectionManager = httpClient.getHttpConnectionManager();
if(connectionManager instanceof MultiThreadedHttpConnectionManager)
{
((MultiThreadedHttpConnectionManager)connectionManager).shutdown();
}
}
}
/**
* get CSV file from remote Url
*
* @param url
* @return
* @throws HttpException
* @throws IOException
*/
byte[] getCSVFile(String url) throws HttpException, IOException {
logger.debug("IN");
HttpClient client = new HttpClient();
HttpConnectionManager conManager = client.getHttpConnectionManager();
// Cancel, proxy settings made on server
// logger.debug("Setting proxy");
// client.getHostConfiguration().setProxy("192.168.10.1", 3128);
// HttpState state = new HttpState();
// state.setProxyCredentials(null, null, new UsernamePasswordCredentials("", ""));
// client.setState(state);
// logger.debug("proxy set");
// cancel
GetMethod httpGet = new GetMethod(url);
int statusCode = client.executeMethod(httpGet);
logger.debug("Status code after request to remote URL is " + statusCode);
byte[] response = httpGet.getResponseBody();
if (response == null) {
logger.warn("Response of remote URL is empty");
}
httpGet.releaseConnection();
logger.debug("OUT");
return response;
}
public OAuthTokenValidationStubFactory(String url, String adminUsername, String adminPassword,
Properties properties) {
this.validateUrl(url);
this.url = url;
this.validateCredentials(adminUsername, adminPassword);
this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes()));
HttpConnectionManager connectionManager = this.createConnectionManager(properties);
this.httpClient = new HttpClient(connectionManager);
}
/**
* Creates an instance of MultiThreadedHttpConnectionManager using HttpClient 3.x APIs
*
* @param properties Properties to configure MultiThreadedHttpConnectionManager
* @return An instance of properly configured MultiThreadedHttpConnectionManager
*/
private HttpConnectionManager createConnectionManager(Properties properties) {
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
if (properties == null || properties.isEmpty()) {
throw new IllegalArgumentException("Parameters required to initialize HttpClient instances " +
"associated with OAuth token validation service stub are not provided");
}
String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost");
if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " +
"which is 2, will be used");
}
} else {
params.setDefaultMaxConnectionsPerHost(Integer.parseInt(maxConnectionsPerHostParam));
}
String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections");
if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " +
"which is 10, will be used");
}
} else {
params.setMaxTotalConnections(Integer.parseInt(maxTotalConnectionsParam));
}
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.setParams(params);
return connectionManager;
}
/**
* Removes the connection manager from this class. The idle connections from the connection
* manager will no longer be automatically closed by this class.
*
* @param connectionManager The connection manager to remove
*/
public synchronized void removeConnectionManager(HttpConnectionManager connectionManager) {
if (shutdown) {
throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
}
this.connectionManagers.remove(connectionManager);
}
/**
* Create an HTTP connection.
*
* @return A HTTP connection.
*/
private static HttpClient createHttpClient(HttpConnectionManager manager) {
HttpClient client = new HttpClient(manager);
client.getParams().setParameter(
HttpMethodParams.USER_AGENT,
"WPCleaner (+http://en.wikipedia.org/wiki/User:NicoV/Wikipedia_Cleaner/Documentation)");
return client;
}
public SegmentValidator(PinotHelixResourceManager pinotHelixResourceManager, ControllerConf controllerConf,
Executor executor, HttpConnectionManager connectionManager, ControllerMetrics controllerMetrics,
boolean isLeaderForTable) {
_pinotHelixResourceManager = pinotHelixResourceManager;
_controllerConf = controllerConf;
_executor = executor;
_connectionManager = connectionManager;
_controllerMetrics = controllerMetrics;
_isLeaderForTable = isLeaderForTable;
}
public TableSizeReader(Executor executor, HttpConnectionManager connectionManager,
ControllerMetrics controllerMetrics, PinotHelixResourceManager helixResourceManager) {
_executor = executor;
_connectionManager = connectionManager;
_controllerMetrics = controllerMetrics;
_helixResourceManager = helixResourceManager;
}
/** @since 2.0 */
@Override
public void closeCommunicationLink() {
if (getClient() != null) {
final HttpConnectionManager mgr = getClient().getHttpConnectionManager();
if (mgr instanceof MultiThreadedHttpConnectionManager) {
((MultiThreadedHttpConnectionManager) mgr).shutdown();
}
}
}
public IdleConnectionMonitorThread(HttpConnectionManager connMgr, long intervalMillis, long maxIdleTimeMillis) {
super();
this.connMgr = connMgr;
this.intervalMillis = intervalMillis;
this.maxIdleTimeMillis = maxIdleTimeMillis;
}
public HttpConnectionManager getConnectionManager()
{
return connectionManager;
}
public void setConnectionManager(HttpConnectionManager connectionManager)
{
this.connectionManager = connectionManager;
}
public ServerTableSizeReader(Executor executor, HttpConnectionManager connectionManager) {
_executor = executor;
_connectionManager = connectionManager;
}
/**
* @param executor executor service to use for making parallel requests
* @param connectionManager http connection manager to use.
*/
public MultiGetRequest(Executor executor, HttpConnectionManager connectionManager) {
_executor = executor;
_connectionManager = connectionManager;
}
@Override
public synchronized HttpConnectionManager getHttpConnectionManager() {
return delegate.getHttpConnectionManager();
}
@Override
public synchronized void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
delegate.setHttpConnectionManager(httpConnectionManager);
}
/**
* Adds a connection manager to be handled by this class.
* {@link HttpConnectionManager#closeIdleConnections(long)} will be called on the connection
* manager every {@link #setTimeoutInterval(long) timeoutInterval} milliseconds.
*
* @param connectionManager The connection manager to add
*/
public synchronized void addConnectionManager(HttpConnectionManager connectionManager) {
if (shutdown) {
throw new IllegalStateException("IdleConnectionTimeoutThread has been shutdown");
}
this.connectionManagers.add(connectionManager);
}
/**
* Handles calling {@link HttpConnectionManager#closeIdleConnections(long) closeIdleConnections()}
* and doing any other cleanup work on the given connection mangaer.
* @param connectionManager The connection manager to close idle connections for
*/
protected void handleCloseIdleConnections(HttpConnectionManager connectionManager) {
connectionManager.closeIdleConnections(connectionTimeout);
}