use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class HostConnections method getConnection.
/**
* Get a connection for the host:port
*
* @return a connection
*/
public NHttpClientConnection getConnection() {
lock.lock();
try {
if (freeConnections.size() > 0) {
if (log.isDebugEnabled()) {
log.debug("Returning an existing free connection " + route);
}
NHttpClientConnection conn = freeConnections.get(0);
freeConnections.remove(conn);
busyConnections.add(conn);
return conn;
}
} finally {
lock.unlock();
}
return null;
}
use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class TargetConnections method getConnection.
/**
* Return a connection to the host:port pair. If a connection is not available
* return <code>null</code>. If the particular host:port allows to create more connections
* this method will try to connect asynchronously. If the connection is successful it will
* be notified in a separate thread.
*
* @param route Http route
* @return Either returns a connection if already available or returns null and notifies
* the delivery agent when the connection is available
*/
public NHttpClientConnection getConnection(HttpRoute route) {
if (log.isDebugEnabled()) {
log.debug("Trying to get a connection " + route);
}
HostConnections pool = getConnectionPool(route);
// trying to get an existing connection
NHttpClientConnection connection = pool.getConnection();
if (connection == null) {
if (pool.canHaveMoreConnections()) {
HttpHost host = route.getProxyHost() != null ? route.getProxyHost() : route.getTargetHost();
ioReactor.connect(new InetSocketAddress(host.getHostName(), host.getPort()), null, pool, callback);
} else {
log.warn("Connection pool reached maximum allowed connections for route " + route + ". Target server may have become slow");
}
}
return connection;
}
use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class TargetConnections method resetConnectionPool.
/**
* Shutdown the connections of the given host:port list. This will allow to create new connection
* at the next request happens.
*
* @param hostList Set of String which contains entries in hots:port format
*/
public void resetConnectionPool(Set<String> hostList) {
for (String host : hostList) {
String[] params = host.split(":");
for (Map.Entry<HttpRoute, HostConnections> connectionsEntry : poolMap.entrySet()) {
HttpRoute httpRoute = connectionsEntry.getKey();
if (params.length > 1 && params[0].equalsIgnoreCase(httpRoute.getTargetHost().getHostName()) && (Integer.valueOf(params[1]) == (httpRoute.getTargetHost().getPort())) && httpRoute.getTargetHost().getSchemeName().equalsIgnoreCase(sslSchemaName)) {
try {
NHttpClientConnection connection = connectionsEntry.getValue().getConnection();
if (connection != null && connection.getContext() != null) {
shutdownConnection(connectionsEntry.getValue().getConnection());
log.info("Connection " + httpRoute.getTargetHost().getHostName() + ":" + httpRoute.getTargetHost().getPort() + " Successful");
} else {
log.debug("Error shutdown connection for " + httpRoute.getTargetHost().getHostName() + " " + httpRoute.getTargetHost().getPort() + " - Connection not available");
}
} catch (Exception e) {
log.warn("Error shutdown connection for " + httpRoute.getTargetHost().getHostName() + " " + httpRoute.getTargetHost().getPort() + " ", e);
}
}
}
}
}
use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class ConnectionPool method getSslConnectionsList.
/**
* Returns SSL Connections List for the given list host:port combinations
*
* @param hostList String List in Host:Port format
* @return List of NHttpClientConnection
*/
public List<NHttpClientConnection> getSslConnectionsList(Set<String> hostList) {
List<NHttpClientConnection> selectedConnections = new ArrayList<NHttpClientConnection>();
for (String host : hostList) {
try {
String[] params = host.split(":");
for (HttpRoute httpRoute : connMap.keySet()) {
if (params.length > 1 && params[0].equalsIgnoreCase(httpRoute.getTargetHost().getHostName()) && (Integer.valueOf(params[1]) == (httpRoute.getTargetHost().getPort())) && httpRoute.getTargetHost().getSchemeName().equalsIgnoreCase(SSL_SCHEMA_NAME)) {
List<NHttpClientConnection> clientConnections = connMap.get(httpRoute);
if (clientConnections != null) {
int count = 0;
for (NHttpClientConnection nHttpClientConnection : clientConnections) {
selectedConnections.add(clientConnections.get(count));
count++;
}
}
}
}
} catch (NumberFormatException e) {
if (log.isWarnEnabled()) {
log.warn("Error in port number in host:port - " + host + " discard connection loading ", e);
}
}
}
return selectedConnections;
}
use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class ConnectionPool method release.
public void release(NHttpClientConnection conn) {
HttpContext ctx = conn.getContext();
Axis2HttpRequest axis2Req = (Axis2HttpRequest) ctx.getAttribute(ClientHandler.AXIS2_HTTP_REQUEST);
HttpRoute route = axis2Req.getRoute();
List<NHttpClientConnection> connections = null;
synchronized (connMap) {
// use double locking to make sure
connections = (List<NHttpClientConnection>) connMap.get(route);
if (connections == null) {
connections = Collections.synchronizedList(new LinkedList<NHttpClientConnection>());
connMap.put(route, connections);
}
}
cleanConnectionReferences(conn);
connections.add(conn);
if (log.isDebugEnabled()) {
log.debug("Released a connection : " + route + " to the connection pool of current size : " + connections.size());
}
}
Aggregations