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

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

Introduction

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

Prototype

public void setDefaultMaxPerRoute(final int max) 

Source Link

Usage

From source file:org.dcache.srm.client.HttpClientSender.java

/**
 * Creates the connection manager to be used to manage connections to SOAP servers.
 *//*from w w  w  .  j av  a  2s.com*/
protected PoolingHttpClientConnectionManager createConnectionManager() {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
            createSocketFactoryRegistry());
    cm.setMaxTotal(clientProperties.getMaximumTotalConnections());
    cm.setDefaultMaxPerRoute(clientProperties.getMaximumConnectionsPerHost());
    SocketConfig.Builder socketOptions = SocketConfig.custom();
    if (clientProperties.getDefaultSoTimeout() > 0) {
        socketOptions.setSoTimeout(clientProperties.getDefaultSoTimeout());
    }
    cm.setDefaultSocketConfig(socketOptions.build());
    return cm;
}

From source file:com.okta.sdk.impl.http.httpclient.HttpClientRequestExecutor.java

/**
 * Creates a new {@code HttpClientRequestExecutor} using the specified {@code ClientCredentials} and optional {@code Proxy}
 * configuration./*from   w  ww.ja  v a  2 s . co m*/
 * @param clientCredentials the Okta account API Key that will be used to authenticate the client with Okta's API sever
 * @param proxy the HTTP proxy to be used when communicating with the Okta API server (can be null)
 * @param authenticationScheme the HTTP authentication scheme to be used when communicating with the Okta API server.
 *                             If null, then SSWS will be used.
 */
public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy proxy,
        AuthenticationScheme authenticationScheme, RequestAuthenticatorFactory requestAuthenticatorFactory,
        Integer connectionTimeout) {
    Assert.notNull(clientCredentials, "clientCredentials argument is required.");
    Assert.isTrue(connectionTimeout >= 0, "Timeout cannot be a negative number.");

    RequestAuthenticatorFactory factory = (requestAuthenticatorFactory != null) ? requestAuthenticatorFactory
            : new DefaultRequestAuthenticatorFactory();

    this.requestAuthenticator = factory.create(authenticationScheme, clientCredentials);

    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();

    if (MAX_CONNECTIONS_TOTAL >= MAX_CONNECTIONS_PER_ROUTE) {
        connMgr.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
        connMgr.setMaxTotal(MAX_CONNECTIONS_TOTAL);
    } else {
        connMgr.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
        connMgr.setMaxTotal(DEFAULT_MAX_CONNECTIONS_TOTAL);

        log.warn(
                "{} ({}) is less than {} ({}). "
                        + "Reverting to defaults: connectionMaxTotal ({}) and connectionMaxPerRoute ({}).",
                MAX_CONNECTIONS_TOTAL_PROPERTY_KEY, MAX_CONNECTIONS_TOTAL,
                MAX_CONNECTIONS_PER_ROUTE_PROPERTY_KEY, MAX_CONNECTIONS_PER_ROUTE,
                DEFAULT_MAX_CONNECTIONS_TOTAL, DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    }

    // The connectionTimeout value is specified in seconds in Okta configuration settings.
    // Therefore, multiply it by 1000 to be milliseconds since RequestConfig expects milliseconds.
    int connectionTimeoutAsMilliseconds = connectionTimeout * 1000;

    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeoutAsMilliseconds)
            .setSocketTimeout(connectionTimeoutAsMilliseconds).setRedirectsEnabled(false).build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
            .disableCookieManagement().setDefaultConnectionConfig(connectionConfig)
            .setConnectionManager(connMgr);

    this.httpClientRequestFactory = new HttpClientRequestFactory(requestConfig);

    if (proxy != null) {
        //We have some proxy setting to use!
        HttpHost httpProxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
        httpClientBuilder.setProxy(httpProxyHost);

        if (proxy.isAuthenticationRequired()) {
            AuthScope authScope = new AuthScope(proxy.getHost(), proxy.getPort());
            Credentials credentials = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword());
            CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider();
            credentialsProviderProvider.setCredentials(authScope, credentials);
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProviderProvider);
        }
    }

    this.httpClient = httpClientBuilder.build();
}

From source file:net.yacy.cora.protocol.http.HTTPClient.java

/**
 * Initialize the maximum connections for the given pool
 * //from   ww  w .j  av  a  2  s  .  c o m
 * @param pool
 *            a pooling connection manager. Must not be null.
 * @param maxConnections.
 *            The new maximum connections values. Must be greater than 0.
 * @throws IllegalArgumentException
 *             when pool is null or when maxConnections is lower than 1
 */
public static void initPoolMaxConnections(final PoolingHttpClientConnectionManager pool, int maxConnections) {
    if (pool == null) {
        throw new IllegalArgumentException("pool parameter must not be null");
    }
    if (maxConnections <= 0) {
        throw new IllegalArgumentException("maxConnections parameter must be greater than zero");
    }
    pool.setMaxTotal(maxConnections);
    // for statistics same value should also be set here
    ConnectionInfo.setMaxcount(maxConnections);

    // connections per host (2 default)
    pool.setDefaultMaxPerRoute((int) (2 * Memory.cores()));

    // Increase max connections for localhost
    final HttpHost localhost = new HttpHost(Domains.LOCALHOST);
    pool.setMaxPerRoute(new HttpRoute(localhost), maxConnections);
}

From source file:com.stormpath.sdk.impl.http.httpclient.HttpClientRequestExecutor.java

/**
 * Creates a new {@code HttpClientRequestExecutor} using the specified {@code ApiKey} and optional {@code Proxy}
 * configuration.//from   w  ww. ja v  a 2 s.  co m
 * @param clientCredentials the Stormpath account API Key that will be used to authenticate the client with Stormpath's API sever
 * @param proxy the HTTP proxy to be used when communicating with the Stormpath API server (can be null)
 * @param authenticationScheme the HTTP authentication scheme to be used when communicating with the Stormpath API server.
 *                             If null, then Sauthc1 will be used.
 */
public HttpClientRequestExecutor(ClientCredentials clientCredentials, Proxy proxy,
        AuthenticationScheme authenticationScheme, RequestAuthenticatorFactory requestAuthenticatorFactory,
        Integer connectionTimeout) {
    Assert.notNull(clientCredentials, "clientCredentials argument is required.");
    Assert.isTrue(connectionTimeout >= 0, "Timeout cannot be a negative number.");

    RequestAuthenticatorFactory factory = (requestAuthenticatorFactory != null) ? requestAuthenticatorFactory
            : new DefaultRequestAuthenticatorFactory();

    this.requestAuthenticator = factory.create(authenticationScheme, clientCredentials);

    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();

    if (MAX_CONNECTIONS_TOTAL >= MAX_CONNECTIONS_PER_ROUTE) {
        connMgr.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
        connMgr.setMaxTotal(MAX_CONNECTIONS_TOTAL);
    } else {
        connMgr.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
        connMgr.setMaxTotal(DEFAULT_MAX_CONNECTIONS_TOTAL);

        log.warn(
                "{} ({}) is less than {} ({}). "
                        + "Reverting to defaults: connectionMaxTotal ({}) and connectionMaxPerRoute ({}).",
                MAX_CONNECTIONS_TOTAL_PROPERTY_KEY, MAX_CONNECTIONS_TOTAL,
                MAX_CONNECTIONS_PER_ROUTE_PROPERTY_KEY, MAX_CONNECTIONS_PER_ROUTE,
                DEFAULT_MAX_CONNECTIONS_TOTAL, DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    }

    // The connectionTimeout value is specified in seconds in Stormpath configuration settings.
    // Therefore, multiply it by 1000 to be milliseconds since RequestConfig expects milliseconds.
    int connectionTimeoutAsMilliseconds = connectionTimeout * 1000;

    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeoutAsMilliseconds)
            .setSocketTimeout(connectionTimeoutAsMilliseconds).setRedirectsEnabled(false).build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
            .disableCookieManagement().setDefaultConnectionConfig(connectionConfig)
            .setConnectionManager(connMgr);

    this.httpClientRequestFactory = new HttpClientRequestFactory(requestConfig);

    if (proxy != null) {
        //We have some proxy setting to use!
        HttpHost httpProxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
        httpClientBuilder.setProxy(httpProxyHost);

        if (proxy.isAuthenticationRequired()) {
            AuthScope authScope = new AuthScope(proxy.getHost(), proxy.getPort());
            Credentials credentials = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword());
            CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider();
            credentialsProviderProvider.setCredentials(authScope, credentials);
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProviderProvider);
        }
    }

    this.httpClient = httpClientBuilder.build();
}

From source file:org.duniter.core.client.service.HttpServiceImpl.java

protected PoolingHttpClientConnectionManager createConnectionManager(int maxTotalConnections,
        int maxConnectionsPerRoute, int timeout) {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(maxTotalConnections);
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
    connectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
    return connectionManager;
}

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  .j a v  a  2 s .  co m
    aConnMgr.setValidateAfterInactivity(1000);

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

    return aConnMgr;
}

From source file:com.microsoft.cognitive.speakerrecognition.SpeakerIdentificationRestClient.java

/**
 * Initializes an instance of the service client
 *
 * @param subscriptionKey The subscription key to use
 *///from  ww w.  j a va 2  s .c om
public SpeakerIdentificationRestClient(String subscriptionKey) {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);

    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    defaultHttpClient = httpClient;//new DefaultHttpClient();
    gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:SS.SSS").create();
    clientHelper = new SpeakerRestClientHelper(subscriptionKey);
}

From source file:com.arpnetworking.metrics.impl.ApacheHttpSink.java

ApacheHttpSink(final Builder builder, final Logger logger) {
    this(builder, new SingletonSupplier<>(() -> {
        final SingletonSupplier<PoolingHttpClientConnectionManager> clientManagerSupplier = new SingletonSupplier<>(
                () -> {//w  w w.j  a  v a 2s  .  co m
                    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
                    connectionManager.setDefaultMaxPerRoute(builder._parallelism);
                    connectionManager.setMaxTotal(builder._parallelism);
                    return connectionManager;
                });

        return HttpClients.custom().setConnectionManager(clientManagerSupplier.get()).build();
    }), logger);
}

From source file:com.hp.mqm.client.AbstractMqmRestClient.java

/**
 * Constructor for AbstractMqmRestClient.
 *
 * @param connectionConfig MQM connection configuration, Fields 'location', 'domain', 'project' and 'clientType' must not be null or empty.
 *///from w w  w  .j a  v a  2 s . c  o m
protected AbstractMqmRestClient(MqmConnectionConfig connectionConfig) {
    checkNotEmpty("Parameter 'location' must not be null or empty.", connectionConfig.getLocation());
    checkNotEmpty("Parameter 'sharedSpace' must not be null or empty.", connectionConfig.getSharedSpace());
    checkNotEmpty("Parameter 'clientType' must not be null or empty.", connectionConfig.getClientType());
    clientType = connectionConfig.getClientType();
    location = connectionConfig.getLocation();
    sharedSpace = connectionConfig.getSharedSpace();
    username = connectionConfig.getUsername();
    password = connectionConfig.getPassword();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(20);
    cm.setDefaultMaxPerRoute(20);
    cookieStore = new BasicCookieStore();

    if (connectionConfig.getProxyHost() != null && !connectionConfig.getProxyHost().isEmpty()) {
        HttpHost proxy = new HttpHost(connectionConfig.getProxyHost(), connectionConfig.getProxyPort());

        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
                .setConnectTimeout(connectionConfig.getDefaultConnectionTimeout() != null
                        ? connectionConfig.getDefaultConnectionTimeout()
                        : DEFAULT_CONNECTION_TIMEOUT)
                .setSocketTimeout(connectionConfig.getDefaultSocketTimeout() != null
                        ? connectionConfig.getDefaultSocketTimeout()
                        : DEFAULT_SO_TIMEOUT)
                .build();

        if (connectionConfig.getProxyCredentials() != null) {
            AuthScope proxyAuthScope = new AuthScope(connectionConfig.getProxyHost(),
                    connectionConfig.getProxyPort());
            Credentials credentials = proxyCredentialsToCredentials(connectionConfig.getProxyCredentials());

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(proxyAuthScope, credentials);

            httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                    .setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(requestConfig)
                    .build();
        } else {
            httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                    .setDefaultRequestConfig(requestConfig).build();
        }
    } else {
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(connectionConfig.getDefaultConnectionTimeout() != null
                        ? connectionConfig.getDefaultConnectionTimeout()
                        : DEFAULT_CONNECTION_TIMEOUT)
                .setSocketTimeout(connectionConfig.getDefaultSocketTimeout() != null
                        ? connectionConfig.getDefaultSocketTimeout()
                        : DEFAULT_SO_TIMEOUT)
                .build();
        httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                .setDefaultRequestConfig(requestConfig).build();
    }
}

From source file:org.ambraproject.wombat.config.RootConfiguration.java

@Bean
public HttpClientConnectionManager httpClientConnectionManager(RuntimeConfiguration runtimeConfiguration) {
    PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();

    final RuntimeConfiguration.HttpConnectionPoolConfiguration httpConnectionPoolConfiguration = runtimeConfiguration
            .getHttpConnectionPoolConfiguration();
    Integer maxTotal = httpConnectionPoolConfiguration.getMaxTotal();
    if (maxTotal != null)
        manager.setMaxTotal(maxTotal);/*from   w w  w .  j  a  va  2 s .c  o  m*/
    Integer defaultMaxPerRoute = httpConnectionPoolConfiguration.getDefaultMaxPerRoute();
    if (defaultMaxPerRoute != null)
        manager.setDefaultMaxPerRoute(defaultMaxPerRoute);

    return manager;
}