Example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager PoolingHttpClientConnectionManager.

Prototype

public PoolingHttpClientConnectionManager(final Registry<ConnectionSocketFactory> socketFactoryRegistry,
            final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) 

Source Link

Usage

From source file:io.mandrel.requests.http.ApacheHttpRequester.java

public void init() {

    available = new Semaphore(maxParallel(), true);

    SSLContext sslContext = SSLContexts.createSystemDefault();
    HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();

    Registry<ConnectionSocketFactory> sessionStrategyRegistry = RegistryBuilder
            .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build();

    DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
        @Override//from   ww  w . j  a v  a 2 s .  c om
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            if (host.equalsIgnoreCase("localhost")) {
                return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) };
            } else {
                return new InetAddress[] { nameResolver().resolve(host) };
            }
        }
    };

    // Create a connection manager with custom configuration.
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
            sessionStrategyRegistry, dnsResolver);

    // Create message constraints
    MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(maxHeaderCount)
            .setMaxLineLength(maxLineLength).build();

    // Create connection configuration
    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8)
            .setMessageConstraints(messageConstraints).build();
    connManager.setDefaultConnectionConfig(connectionConfig);

    // Configure total max or per route limits for persistent connections
    // that can be kept in the pool or leased by the connection manager.
    connManager.setMaxTotal(maxPersistentConnections());
    connManager.setDefaultMaxPerRoute(maxPersistentConnections());

    // TODO
    // Use custom credentials provider if necessary.
    // CredentialsProvider credentialsProvider = new
    // BasicCredentialsProvider();

    // Create global request configuration
    defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
            .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
            .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setMaxRedirects(maxRedirects())
            .setSocketTimeout(socketTimeout()).setConnectTimeout(connectTimeout())
            .setConnectionRequestTimeout(requestTimeOut()).setRedirectsEnabled(followRedirects()).build();

    // Create an HttpClient with the given custom dependencies and
    // configuration.
    client = HttpClients.custom().setConnectionManager(connManager)
            // .setDefaultCredentialsProvider(credentialsProvider)
            .setDefaultRequestConfig(defaultRequestConfig).build();
}

From source file:org.codelibs.robot.S2RobotTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();

    final Map<String, String> featureMap = newHashMap();
    featureMap.put("http://xml.org/sax/features/namespaces", "false");
    final Map<String, String> propertyMap = newHashMap();
    final Map<String, String> childUrlRuleMap = newHashMap();
    childUrlRuleMap.put("//A", "href");
    childUrlRuleMap.put("//AREA", "href");
    childUrlRuleMap.put("//FRAME", "src");
    childUrlRuleMap.put("//IFRAME", "src");
    childUrlRuleMap.put("//IMG", "src");
    childUrlRuleMap.put("//LINK", "href");
    childUrlRuleMap.put("//SCRIPT", "src");

    container = new StandardRobotContainer();
    container.<HcHttpClient>prototype("internalHttpClient", HcHttpClient.class, client -> {
        client.setCookieSpec(CookieSpecs.BEST_MATCH);
        client.setClientConnectionManager(container.getComponent("clientConnectionManager"));
    }).prototype("httpClient", FaultTolerantClient.class, client -> {
        client.setRobotClient(container.getComponent("internalHttpClient"));
        client.setMaxRetryCount(5);/*from   ww w.jav  a 2  s . c  om*/
        client.setRetryInterval(500);
    }).prototype("fsClient", FileSystemClient.class)
            .prototype("ruleManager", RuleManagerImpl.class, manager -> {
                manager.addRule(container.getComponent("sitemapsRule"));
                manager.addRule(container.getComponent("fileRule"));
            }).prototype("accessResult", AccessResultImpl.class).prototype("urlQueue", UrlQueueImpl.class)
            .prototype("robotThread", S2RobotThread.class).prototype("s2Robot", S2Robot.class)
            .prototype("urlFilterService", UrlFilterServiceImpl.class)
            .prototype("urlQueueService", UrlQueueServiceImpl.class)
            .prototype("dataService", DataServiceImpl.class).prototype("urlFilter", UrlFilterImpl.class)
            .singleton("urlConvertHelper", UrlConvertHelper.class)
            .singleton("intervalController", DefaultIntervalController.class)
            .singleton("sitemapsHelper", SitemapsHelper.class).singleton("logHelper", LogHelperImpl.class)
            .singleton("encodingHelper", EncodingHelper.class)
            .singleton("contentLengthHelper", ContentLengthHelper.class)
            .singleton("mimeTypeHelper", MimeTypeHelperImpl.class)
            .<FileTransformer>singleton("fileTransformer", FileTransformer.class, transformer -> {
                transformer.setName("fileTransformer");
                transformer.setFeatureMap(featureMap);
                transformer.setPropertyMap(propertyMap);
                transformer.setChildUrlRuleMap(childUrlRuleMap);
            }).singleton("dataHelper", MemoryDataHelper.class)
            .singleton("robotsTxtHelper", RobotsTxtHelper.class)
            .<S2RobotClientFactory>singleton("clientFactory", S2RobotClientFactory.class, factory -> {
                factory.addClient("http:.*", container.getComponent("httpClient"));
                factory.addClient("file:.*", container.getComponent("fsClient"));
            }).singleton("tikaExtractor", TikaExtractor.class)
            .<ExtractorFactory>singleton("extractorFactory", ExtractorFactory.class, factory -> {
                TikaExtractor tikaExtractor = container.getComponent("tikaExtractor");
                factory.addExtractor("text/plain", tikaExtractor);
                factory.addExtractor("text/html", tikaExtractor);
            })//
            .singleton("httpClient", HcHttpClient.class)//
            .singleton("sitemapsResponseProcessor", SitemapsResponseProcessor.class)//
            .<SitemapsRule>singleton("sitemapsRule", SitemapsRule.class, rule -> {
                rule.setResponseProcessor(container.getComponent("sitemapsResponseProcessor"));
                rule.setRuleId("sitemapsRule");
                rule.addRule("url", ".*sitemap.*");
            })//
            .<DefaultResponseProcessor>singleton("defaultResponseProcessor", DefaultResponseProcessor.class,
                    processor -> {
                        processor.setTransformer(container.getComponent("fileTransformer"));
                        processor.setSuccessfulHttpCodes(new int[] { 200 });
                        processor.setNotModifiedHttpCodes(new int[] { 304 });
                    })//
            .<RegexRule>singleton("fileRule", RegexRule.class, rule -> {
                rule.setRuleId("fileRule");
                rule.setDefaultRule(true);
                rule.setResponseProcessor(container.getComponent("defaultResponseProcessor"));
            })//
            .<PoolingHttpClientConnectionManager>singleton("clientConnectionManager",
                    new PoolingHttpClientConnectionManager(5, TimeUnit.MINUTES), manager -> {
                        manager.setMaxTotal(200);
                        manager.setDefaultMaxPerRoute(20);
                    });

    s2Robot = container.getComponent("s2Robot");
    dataService = container.getComponent("dataService");
    urlQueueService = container.getComponent("urlQueueService");
    fileTransformer = container.getComponent("fileTransformer");

}

From source file:org.thevortex.lighting.jinks.client.WinkClient.java

/**
 * Basic setup for a client builder, including the connection manager.
 *
 * @return the builder/*w w  w.  j a va  2 s  .com*/
 */
public synchronized CloseableHttpClient getClient() {
    if (httpClient == null) {
        PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager(1, TimeUnit.MINUTES);
        mgr.setDefaultMaxPerRoute(5);
        mgr.setMaxTotal(15);

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(mgr);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout * 1000)
                .setSocketTimeout(socketTimeout * 1000).build();
        httpClient = httpClientBuilder.setDefaultRequestConfig(requestConfig).build();
    }
    return httpClient;
}

From source file:com.helger.httpclient.HttpClientFactory.java

@Nonnull
public HttpClientConnectionManager createConnectionManager(
        @Nonnull final LayeredConnectionSocketFactory aSSLFactory) {
    final Registry<ConnectionSocketFactory> aConSocketRegistry = RegistryBuilder
            .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", aSSLFactory).build();
    final DnsResolver aDNSResolver = createDNSResolver();
    final PoolingHttpClientConnectionManager aConnMgr = new PoolingHttpClientConnectionManager(
            aConSocketRegistry, aDNSResolver);
    aConnMgr.setDefaultMaxPerRoute(100);
    aConnMgr.setMaxTotal(200);//from   w  ww  .ja  v a2 s . c o m
    aConnMgr.setValidateAfterInactivity(1000);

    final ConnectionConfig aConnectionConfig = createConnectionConfig();
    aConnMgr.setDefaultConnectionConfig(aConnectionConfig);

    return aConnMgr;
}

From source file:org.iipg.hurricane.jmx.client.JMXClientBuilder.java

private PoolingHttpClientConnectionManager createPoolingConnectionManager() {
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
            getSocketFactoryRegistry(), getConnectionFactory());
    connManager.setDefaultSocketConfig(createSocketConfig());
    connManager.setDefaultConnectionConfig(createConnectionConfig());
    if (maxTotalConnections != 0) {
        connManager.setMaxTotal(maxTotalConnections);
    }//from   ww w  . j a v a  2  s .c om
    return connManager;
}

From source file:org.glassfish.jersey.apache.connector.ApacheConnector.java

private HttpClientConnectionManager createConnectionManager(final Client client, final Configuration config,
        final SSLContext sslContext, final boolean useSystemProperties) {

    final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols"))
            : null;/*from   w  w w.  ja v a 2 s  .c  o  m*/
    final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites"))
            : null;

    HostnameVerifier hostnameVerifier = client.getHostnameVerifier();

    final LayeredConnectionSocketFactory sslSocketFactory;
    if (sslContext != null) {
        sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites,
                hostnameVerifier);
    } else {
        if (useSystemProperties) {
            sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(),
                    supportedProtocols, supportedCipherSuites, hostnameVerifier);
        } else {
            sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
        }
    }

    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    final Integer chunkSize = ClientProperties.getValue(config.getProperties(),
            ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.class);

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            registry, new ConnectionFactory(chunkSize));

    if (useSystemProperties) {
        String s = System.getProperty("http.keepAlive", "true");
        if ("true".equalsIgnoreCase(s)) {
            s = System.getProperty("http.maxConnections", "5");
            final int max = Integer.parseInt(s);
            connectionManager.setDefaultMaxPerRoute(max);
            connectionManager.setMaxTotal(2 * max);
        }
    }

    return connectionManager;
}

From source file:com.liferay.sync.engine.session.Session.java

private HttpClientConnectionManager _getHttpClientConnectionManager(boolean trustSelfSigned) {

    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new SyncManagedHttpClientConnectionFactory();

    try {//w ww. j  a v a  2  s.  c  o  m
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();

        registryBuilder.register("http", new PlainConnectionSocketFactory());

        if (trustSelfSigned) {
            registryBuilder.register("https", _getTrustingSSLSocketFactory());
        } else {
            registryBuilder.register("https", _getDefaultSSLSocketFactory());
        }

        Registry<ConnectionSocketFactory> registry = registryBuilder.build();

        return new PoolingHttpClientConnectionManager(registry, connectionFactory);
    } catch (Exception e) {
        _logger.error(e.getMessage(), e);
    }

    return new PoolingHttpClientConnectionManager(connectionFactory);
}

From source file:com.github.dockerjava.jaxrs.connector.ApacheConnector.java

private HttpClientConnectionManager createConnectionManager(final Configuration config,
        final SSLContext sslContext, X509HostnameVerifier hostnameVerifier, final boolean useSystemProperties) {

    final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols"))
            : null;/* w w  w  .  j  a v a2 s  . c om*/
    final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites"))
            : null;

    if (hostnameVerifier == null) {
        hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
    }

    final LayeredConnectionSocketFactory sslSocketFactory;
    if (sslContext != null) {
        sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites,
                hostnameVerifier);
    } else {
        if (useSystemProperties) {
            sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(),
                    supportedProtocols, supportedCipherSuites, hostnameVerifier);
        } else {
            sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
        }
    }

    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    final Integer chunkSize = ClientProperties.getValue(config.getProperties(),
            ClientProperties.CHUNKED_ENCODING_SIZE, 4096, Integer.class);

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            registry, new ConnectionFactory(chunkSize));

    if (useSystemProperties) {
        String s = System.getProperty("http.keepAlive", "true");
        if ("true".equalsIgnoreCase(s)) {
            s = System.getProperty("http.maxConnections", "5");
            final int max = Integer.parseInt(s);
            connectionManager.setDefaultMaxPerRoute(max);
            connectionManager.setMaxTotal(2 * max);
        }
    }

    return connectionManager;
}

From source file:run.var.teamcity.cloud.docker.client.apcon.ApacheConnector.java

private HttpClientConnectionManager createConnectionManager(final Client client, final Configuration config,
        final SSLContext sslContext, final boolean useSystemProperties) {

    final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols"))
            : null;//from  ww w.ja v  a  2s .  c o m
    final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites"))
            : null;

    HostnameVerifier hostnameVerifier = client.getHostnameVerifier();

    final LayeredConnectionSocketFactory sslSocketFactory;
    if (sslContext != null) {
        sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites,
                hostnameVerifier);
    } else {
        if (useSystemProperties) {
            sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(),
                    supportedProtocols, supportedCipherSuites, hostnameVerifier);
        } else {
            sslSocketFactory = new SSLConnectionSocketFactory(
                    org.apache.http.conn.ssl.SSLContexts.createDefault(), hostnameVerifier);
        }
    }

    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    final Integer chunkSize = ClientProperties.getValue(config.getProperties(),
            ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.class);

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            registry, new ConnectionFactory(chunkSize));

    if (useSystemProperties) {
        String s = System.getProperty("http.keepAlive", "true");
        if ("true".equalsIgnoreCase(s)) {
            s = System.getProperty("http.maxConnections", "5");
            final int max = Integer.parseInt(s);
            connectionManager.setDefaultMaxPerRoute(max);
            connectionManager.setMaxTotal(2 * max);
        }
    }

    return connectionManager;
}