Ported HttpConnectionManagerParams, MultiThreadedHttpConnectionManager and IdleConnectionHandler classes from HttpClient 3.x to the new API
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@486244 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
87ae8567e7
commit
c76c1a0d7e
|
@ -33,9 +33,13 @@ import java.io.IOException;
|
|||
import java.net.SocketException;
|
||||
|
||||
import org.apache.http.HttpClientConnection;
|
||||
import org.apache.http.HttpInetConnection;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.params.HttpParams;
|
||||
|
||||
public interface HttpHostConnection extends HttpClientConnection {
|
||||
public interface HttpHostConnection extends HttpClientConnection, HttpInetConnection {
|
||||
|
||||
void setHttpConnectionManager(HttpConnectionManager manager);
|
||||
|
||||
HostConfiguration getHostConfiguration();
|
||||
|
||||
|
@ -45,4 +49,14 @@ public interface HttpHostConnection extends HttpClientConnection {
|
|||
|
||||
void setSocketTimeout(int timeout) throws SocketException;
|
||||
|
||||
HttpResponse getLastResponse();
|
||||
|
||||
void setLastResponse(HttpResponse response);
|
||||
|
||||
void setLocked(boolean locked);
|
||||
|
||||
boolean isLocked();
|
||||
|
||||
void releaseConnection();
|
||||
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class DefaultHttpHostConnection
|
|||
private static final Log LOG = LogFactory.getLog(DefaultHttpHostConnection.class);
|
||||
|
||||
/** the connection manager that created this connection or null */
|
||||
private final HttpConnectionManager manager;
|
||||
private HttpConnectionManager manager;
|
||||
|
||||
private HostConfiguration hostconf;
|
||||
|
||||
|
@ -80,11 +80,14 @@ public class DefaultHttpHostConnection
|
|||
|
||||
private HttpResponse lastResponse;
|
||||
|
||||
public DefaultHttpHostConnection(final HttpConnectionManager manager) {
|
||||
public DefaultHttpHostConnection() {
|
||||
super();
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void setHttpConnectionManager(final HttpConnectionManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void setHostConfiguration(final HostConfiguration hostconf) {
|
||||
assertNotOpen();
|
||||
this.hostconf = hostconf;
|
||||
|
@ -246,7 +249,7 @@ public class DefaultHttpHostConnection
|
|||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
protected boolean isLocked() {
|
||||
public boolean isLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
|
@ -259,7 +262,7 @@ public class DefaultHttpHostConnection
|
|||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
protected void setLocked(boolean locked) {
|
||||
public void setLocked(boolean locked) {
|
||||
this.locked = locked;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.http.conn.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.conn.HttpHostConnection;
|
||||
|
||||
/**
|
||||
* A helper class for connection managers to track idle connections.
|
||||
*
|
||||
* <p>This class is not synchronized.</p>
|
||||
*
|
||||
* @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public class IdleConnectionHandler {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
|
||||
|
||||
/** Holds connections and the time they were added. */
|
||||
private Map connectionToAdded = new HashMap();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public IdleConnectionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the given connection with this handler. The connection will be held until
|
||||
* {@link #remove(HttpConnection)} or {@link #closeIdleConnections(long)} is called.
|
||||
*
|
||||
* @param connection the connection to add
|
||||
*
|
||||
* @see #remove(HttpConnection)
|
||||
*/
|
||||
public void add(HttpHostConnection connection) {
|
||||
|
||||
Long timeAdded = new Long(System.currentTimeMillis());
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Adding connection at: " + timeAdded);
|
||||
}
|
||||
|
||||
connectionToAdded.put(connection, timeAdded);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given connection from the list of connections to be closed when idle.
|
||||
* @param connection
|
||||
*/
|
||||
public void remove(HttpHostConnection connection) {
|
||||
connectionToAdded.remove(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all connections referenced by this handler.
|
||||
*/
|
||||
public void removeAll() {
|
||||
this.connectionToAdded.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes connections that have been idle for at least the given amount of time.
|
||||
*
|
||||
* @param idleTime the minimum idle time, in milliseconds, for connections to be closed
|
||||
*/
|
||||
public void closeIdleConnections(long idleTime) {
|
||||
|
||||
// the latest time for which connections will be closed
|
||||
long idleTimeout = System.currentTimeMillis() - idleTime;
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Checking for connections, idleTimeout: " + idleTimeout);
|
||||
}
|
||||
|
||||
Iterator connectionIter = connectionToAdded.keySet().iterator();
|
||||
|
||||
while (connectionIter.hasNext()) {
|
||||
HttpHostConnection conn = (HttpHostConnection) connectionIter.next();
|
||||
Long connectionTime = (Long) connectionToAdded.get(conn);
|
||||
if (connectionTime.longValue() <= idleTimeout) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Closing connection, connection time: " + connectionTime);
|
||||
}
|
||||
connectionIter.remove();
|
||||
try {
|
||||
conn.close();
|
||||
} catch (IOException ex) {
|
||||
LOG.debug("I/O error closing connection", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -69,7 +69,7 @@ public class SimpleHttpConnectionManager implements HttpConnectionManager {
|
|||
* consume it now.
|
||||
* @param conn The connection
|
||||
*/
|
||||
private void finishLastResponse(DefaultHttpHostConnection conn) {
|
||||
protected static void finishLastResponse(HttpHostConnection conn) {
|
||||
HttpResponse lastResponse = conn.getLastResponse();
|
||||
if (lastResponse != null) {
|
||||
conn.setLastResponse(null);
|
||||
|
@ -158,7 +158,8 @@ public class SimpleHttpConnectionManager implements HttpConnectionManager {
|
|||
HostConfiguration hostConfiguration, long timeout) {
|
||||
|
||||
if (httpConnection == null) {
|
||||
httpConnection = new DefaultHttpHostConnection(this);
|
||||
httpConnection = new DefaultHttpHostConnection();
|
||||
httpConnection.setHttpConnectionManager(this);
|
||||
} else {
|
||||
// make sure the host and proxy are correct for this connection
|
||||
// close it and set the values if they are not
|
||||
|
|
|
@ -0,0 +1,222 @@
|
|||
/*
|
||||
* $HeadURL$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.conn.params;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.conn.HostConfiguration;
|
||||
import org.apache.http.params.HttpParams;
|
||||
|
||||
/**
|
||||
* This class represents a collection of HTTP protocol parameters applicable to
|
||||
* {@link HttpConnectionManager HTTP connection managers}.
|
||||
* Protocol parameters may be linked together to form a hierarchy. If a particular
|
||||
* parameter value has not been explicitly defined in the collection itself, its
|
||||
* value will be drawn from the parent collection of parameters.
|
||||
*
|
||||
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
||||
* @author Michael Becke
|
||||
*
|
||||
* @version $Revision$
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public final class HttpConnectionManagerParams {
|
||||
|
||||
/** The default maximum number of connections allowed per host */
|
||||
public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2; // Per RFC 2616 sec 8.1.4
|
||||
|
||||
/** The default maximum number of connections allowed overall */
|
||||
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;
|
||||
|
||||
/**
|
||||
* Defines the maximum number of connections allowed per host configuration.
|
||||
* These values only apply to the number of connections from a particular instance
|
||||
* of HttpConnectionManager.
|
||||
* <p>
|
||||
* This parameter expects a value of type {@link java.util.Map}. The value
|
||||
* should map instances of {@link HostConfiguration}
|
||||
* to {@link Integer integers}. The default value can be specified using
|
||||
* {@link HostConfiguration#ANY_HOST_CONFIGURATION}.
|
||||
* </p>
|
||||
*/
|
||||
public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
|
||||
|
||||
/**
|
||||
* Defines the maximum number of connections allowed overall. This value only applies
|
||||
* to the number of connections from a particular instance of HttpConnectionManager.
|
||||
* <p>
|
||||
* This parameter expects a value of type {@link Integer}.
|
||||
* </p>
|
||||
*/
|
||||
public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
|
||||
|
||||
/**
|
||||
* Sets the default maximum number of connections allowed for a given
|
||||
* host config.
|
||||
*
|
||||
* @param maxHostConnections The default maximum.
|
||||
*
|
||||
* @see #MAX_HOST_CONNECTIONS
|
||||
*/
|
||||
public static void setDefaultMaxConnectionsPerHost(
|
||||
final HttpParams params,
|
||||
int maxHostConnections) {
|
||||
setMaxConnectionsPerHost(
|
||||
params,
|
||||
HostConfiguration.ANY_HOST_CONFIGURATION,
|
||||
maxHostConnections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum number of connections to be used for the given host config.
|
||||
*
|
||||
* @param hostConfiguration The host config to set the maximum for. Use
|
||||
* {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
|
||||
* per host.
|
||||
* @param maxHostConnections The maximum number of connections, <code>> 0</code>
|
||||
*
|
||||
* @see #MAX_HOST_CONNECTIONS
|
||||
*/
|
||||
public static void setMaxConnectionsPerHost(
|
||||
final HttpParams params,
|
||||
final HostConfiguration hostConfiguration,
|
||||
int maxHostConnections) {
|
||||
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
if (maxHostConnections <= 0) {
|
||||
throw new IllegalArgumentException("maxHostConnections must be greater than 0");
|
||||
}
|
||||
|
||||
Map currentValues = (Map) params.getParameter(MAX_HOST_CONNECTIONS);
|
||||
// param values are meant to be immutable so we'll make a copy
|
||||
// to modify
|
||||
Map newValues = null;
|
||||
if (currentValues == null) {
|
||||
newValues = new HashMap();
|
||||
} else {
|
||||
newValues = new HashMap(currentValues);
|
||||
}
|
||||
newValues.put(hostConfiguration, new Integer(maxHostConnections));
|
||||
params.setParameter(MAX_HOST_CONNECTIONS, newValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default maximum number of connections allowed for a given
|
||||
* host config.
|
||||
*
|
||||
* @return The default maximum.
|
||||
*
|
||||
* @see #MAX_HOST_CONNECTIONS
|
||||
*/
|
||||
public static int getDefaultMaxConnectionsPerHost(
|
||||
final HttpParams params) {
|
||||
return getMaxConnectionsPerHost(
|
||||
params,
|
||||
HostConfiguration.ANY_HOST_CONFIGURATION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum number of connections to be used for a particular host config. If
|
||||
* the value has not been specified for the given host the default value will be
|
||||
* returned.
|
||||
*
|
||||
* @param hostConfiguration The host config.
|
||||
* @return The maximum number of connections to be used for the given host config.
|
||||
*
|
||||
* @see #MAX_HOST_CONNECTIONS
|
||||
*/
|
||||
public static int getMaxConnectionsPerHost(
|
||||
final HttpParams params,
|
||||
final HostConfiguration hostConfiguration) {
|
||||
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
Map m = (Map) params.getParameter(MAX_HOST_CONNECTIONS);
|
||||
if (m == null) {
|
||||
// MAX_HOST_CONNECTIONS have not been configured, using the default value
|
||||
return DEFAULT_MAX_HOST_CONNECTIONS;
|
||||
} else {
|
||||
Integer max = (Integer) m.get(hostConfiguration);
|
||||
if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
|
||||
// the value has not been configured specifically for this host config,
|
||||
// use the default value
|
||||
return getMaxConnectionsPerHost(params, HostConfiguration.ANY_HOST_CONFIGURATION);
|
||||
} else {
|
||||
return (
|
||||
max == null
|
||||
? DEFAULT_MAX_HOST_CONNECTIONS
|
||||
: max.intValue()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum number of connections allowed.
|
||||
*
|
||||
* @param maxTotalConnections The maximum number of connections allowed.
|
||||
*
|
||||
* @see #MAX_TOTAL_CONNECTIONS
|
||||
*/
|
||||
public static void setMaxTotalConnections(
|
||||
final HttpParams params,
|
||||
int maxTotalConnections) {
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
params.setIntParameter(
|
||||
HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
|
||||
maxTotalConnections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum number of connections allowed.
|
||||
*
|
||||
* @return The maximum number of connections allowed.
|
||||
*
|
||||
* @see #MAX_TOTAL_CONNECTIONS
|
||||
*/
|
||||
public static int getMaxTotalConnections(
|
||||
final HttpParams params) {
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
return params.getIntParameter(
|
||||
HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
|
||||
DEFAULT_MAX_TOTAL_CONNECTIONS);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue