Search in sources :

Example 6 with NHttpClientConnection

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;
}
Also used : NHttpClientConnection(org.apache.http.nio.NHttpClientConnection)

Example 7 with NHttpClientConnection

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;
}
Also used : HttpHost(org.apache.http.HttpHost) InetSocketAddress(java.net.InetSocketAddress) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection)

Example 8 with NHttpClientConnection

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);
                }
            }
        }
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) IOException(java.io.IOException)

Example 9 with NHttpClientConnection

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;
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) ArrayList(java.util.ArrayList) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection)

Example 10 with NHttpClientConnection

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());
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpContext(org.apache.http.protocol.HttpContext) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) LinkedList(java.util.LinkedList)

Aggregations

NHttpClientConnection (org.apache.http.nio.NHttpClientConnection)14 HttpRoute (org.apache.http.conn.routing.HttpRoute)6 TargetConfiguration (org.apache.synapse.transport.passthru.config.TargetConfiguration)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)5 NativeWorkerPool (org.apache.axis2.transport.base.threads.NativeWorkerPool)5 WorkerPool (org.apache.axis2.transport.base.threads.WorkerPool)5 PassThroughTransportMetricsCollector (org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector)5 MessageContext (org.apache.axis2.context.MessageContext)3 HttpHost (org.apache.http.HttpHost)3 HttpResponse (org.apache.http.HttpResponse)3 HttpContext (org.apache.http.protocol.HttpContext)3 ClientConnFactory (org.apache.synapse.transport.http.conn.ClientConnFactory)3 InetSocketAddress (java.net.InetSocketAddress)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2