Search in sources :

Example 11 with HttpConnectionManagerParams

use of org.apache.commons.httpclient.params.HttpConnectionManagerParams in project ovirt-engine by oVirt.

the class HttpUtils method getConnection.

/**
 * There are places in the code where use http to communicate with vdsm or external providers
 *
 * @param connectionTimeOut
 *            - the instance type of the interface for this connection
 * @param clientRetries
 *            - Number of retries if timeout occurd
 * @param maxConnectionsPerHost
 *            - maximum number of connections allowed for a given host
 * @param maxTotalConnections
 *            - The maximum number of connections allowed
 * @return {@link HttpClient}.
 */
public static HttpClient getConnection(int connectionTimeOut, int clientRetries, int maxConnectionsPerHost, int maxTotalConnections) {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(connectionTimeOut);
    params.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
    params.setMaxTotalConnections(maxTotalConnections);
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.setParams(params);
    // Create the client:
    HttpClient client = new HttpClient(httpConnectionManager);
    // Configure the HTTP client so it will retry the execution of
    // methods when there are IO errors:
    int retries = Config.getValue(ConfigValues.vdsRetries);
    HttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(retries, false);
    HttpClientParams parameters = client.getParams();
    parameters.setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    // Done:
    return client;
}
Also used : HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 12 with HttpConnectionManagerParams

use of org.apache.commons.httpclient.params.HttpConnectionManagerParams in project wicket by apache.

the class Tester method run.

/**
 * Runs the test.
 *
 * @throws Exception
 */
public void run() throws Exception {
    activeThreads = 0;
    HttpConnectionManagerParams connManagerParams = new HttpConnectionManagerParams();
    connManagerParams.setDefaultMaxConnectionsPerHost(numberOfThreads * 2);
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    manager.setParams(connManagerParams);
    Server server = null;
    GetMethod getMethod = new GetMethod("http://localhost:" + port + "/");
    try {
        getMethod.setFollowRedirects(true);
        HttpClient httpClient = new HttpClient(params, manager);
        int code = httpClient.executeMethod(getMethod);
        if (code != 200) {
            server = startServer(port);
        }
    } catch (Exception e) {
        server = startServer(port);
    } finally {
        getMethod.releaseConnection();
    }
    try {
        ThreadGroup g = new ThreadGroup("runners");
        Thread[] threads = new Thread[numberOfThreads];
        HttpClient client = null;
        for (int i = 0; i < numberOfThreads; i++) {
            if (multipleSessions) {
                client = new HttpClient(params, manager);
                client.getHostConfiguration().setHost(host, port);
            } else {
                if (client == null) {
                    client = new HttpClient(params, manager);
                    client.getHostConfiguration().setHost(host, port);
                }
            }
            threads[i] = new Thread(g, new CommandRunner(commands, client, this));
        }
        long start = System.currentTimeMillis();
        for (int i = 0; i < numberOfThreads; i++) {
            activeThreads++;
            threads[i].start();
        }
        while (activeThreads > 0) {
            synchronized (this) {
                wait();
            }
        }
        long end = System.currentTimeMillis();
        long time = end - start;
        log.info("\n******** finished in " + Duration.milliseconds(time) + " (" + time + " milis)");
    } finally {
        MultiThreadedHttpConnectionManager.shutdownAll();
        if (server != null) {
            server.stop();
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 13 with HttpConnectionManagerParams

use of org.apache.commons.httpclient.params.HttpConnectionManagerParams in project nutch by apache.

the class Http method configureClient.

/**
 * Configures the HTTP client
 */
private void configureClient() {
    // Set up an HTTPS socket factory that accepts self-signed certs.
    ProtocolSocketFactory factory;
    if (tlsCheckCertificate) {
        factory = new SSLProtocolSocketFactory();
    } else {
        factory = new DummySSLProtocolSocketFactory();
    }
    Protocol https = new Protocol("https", factory, 443);
    Protocol.registerProtocol("https", https);
    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setConnectionTimeout(timeout);
    params.setSoTimeout(timeout);
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    // --------------------------------------------------------------------------------
    // NUTCH-1836: Modification to increase the number of available connections
    // for multi-threaded crawls.
    // --------------------------------------------------------------------------------
    params.setMaxTotalConnections(conf.getInt("mapreduce.tasktracker.map.tasks.maximum", 5) * conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // Also set max connections per host to maxThreadsTotal since all threads
    // might be used to fetch from the same host - otherwise timeout errors can
    // occur
    params.setDefaultMaxConnectionsPerHost(conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // executeMethod(HttpMethod) seems to ignore the connection timeout on the
    // connection manager.
    // set it explicitly on the HttpClient.
    client.getParams().setConnectionManagerTimeout(timeout);
    HostConfiguration hostConf = client.getHostConfiguration();
    ArrayList<Header> headers = new ArrayList<Header>();
    // Note: some header fields (e.g., "User-Agent") are set per GET request
    if (!acceptLanguage.isEmpty()) {
        headers.add(new Header("Accept-Language", acceptLanguage));
    }
    if (!acceptCharset.isEmpty()) {
        headers.add(new Header("Accept-Charset", acceptCharset));
    }
    if (!accept.isEmpty()) {
        headers.add(new Header("Accept", accept));
    }
    // accept gzipped content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
    hostConf.getParams().setParameter("http.default-headers", headers);
    // HTTP proxy server details
    if (useProxy) {
        hostConf.setProxy(proxyHost, proxyPort);
        if (proxyUsername.length() > 0) {
            AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm);
            NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword, Http.agentHost, this.proxyRealm);
            client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }
}
Also used : SSLProtocolSocketFactory(org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) SSLProtocolSocketFactory(org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory) Header(org.apache.commons.httpclient.Header) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) ArrayList(java.util.ArrayList) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) NTCredentials(org.apache.commons.httpclient.NTCredentials)

Example 14 with HttpConnectionManagerParams

use of org.apache.commons.httpclient.params.HttpConnectionManagerParams in project coprhd-controller by CoprHD.

the class HDSApiFactory method init.

/**
 * Initialize HTTP client
 */
public void init() {
    // Create the VPlex API client map.
    _clientMap = new ConcurrentHashMap<String, HDSApiClient>();
    // Setup the connection parameters.
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxTotalConnections(_maxConn);
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setConnectionTimeout(_connTimeoutMs);
    params.setSoTimeout(socketConnectionTimeoutMs);
    params.setTcpNoDelay(true);
    // Create the HTTP connection manager for managing the set of HTTP
    // connections.
    MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
    mgr.setParams(params);
    // close idle connections immediately
    mgr.closeIdleConnections(0);
    // Create the HTTP client and set the handler for determining if an
    // HttpMethod should be retried after a recoverable exception during
    // execution.
    HttpClient client = new HttpClient(mgr);
    client.getParams().setConnectionManagerTimeout(connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });
    // Create the client handler.
    _clientHandler = new ApacheHttpClientHandler(client);
    // Register the specific for the HTTPS protocol.
    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 443));
}
Also used : IOException(java.io.IOException) HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) ApacheHttpClientHandler(com.sun.jersey.client.apache.ApacheHttpClientHandler) Protocol(org.apache.commons.httpclient.protocol.Protocol) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 15 with HttpConnectionManagerParams

use of org.apache.commons.httpclient.params.HttpConnectionManagerParams in project coprhd-controller by CoprHD.

the class HP3PARApiFactory method init.

/**
 * Initialize HTTP client
 */
public void init() {
    _log.info("3PARDriver:HP3PARApiFactory init enter");
    _clientMap = new ConcurrentHashMap<String, HP3PARApi>();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);
    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    // close idle connections immediately
    _connectionManager.closeIdleConnections(0);
    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });
    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 8080));
}
Also used : IOException(java.io.IOException) HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HP3PARApi(com.emc.storageos.hp3par.impl.HP3PARApi) Protocol(org.apache.commons.httpclient.protocol.Protocol) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)19 HttpClient (org.apache.commons.httpclient.HttpClient)15 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)13 IOException (java.io.IOException)8 HttpMethodRetryHandler (org.apache.commons.httpclient.HttpMethodRetryHandler)7 Protocol (org.apache.commons.httpclient.protocol.Protocol)7 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 ApacheHttpClientHandler (com.sun.jersey.client.apache.ApacheHttpClientHandler)5 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)5 HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)3 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)2 HP3PARApi (com.emc.storageos.hp3par.impl.HP3PARApi)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)1 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)1 File (java.io.File)1 URI (java.net.URI)1 GeneralSecurityException (java.security.GeneralSecurityException)1 X509Certificate (java.security.cert.X509Certificate)1