Example usage for org.apache.http.impl.client HttpClientBuilder setMaxConnPerRoute

List of usage examples for org.apache.http.impl.client HttpClientBuilder setMaxConnPerRoute

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder setMaxConnPerRoute.

Prototype

public final HttpClientBuilder setMaxConnPerRoute(final int maxConnPerRoute) 

Source Link

Document

Assigns maximum connection per route value.

Usage

From source file:sk.datalan.solr.impl.HttpClientUtil.java

public static HttpClientBuilder configureClient(final HttpClientConfiguration config) {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // max total connections
    if (config.isSetMaxConnections()) {
        clientBuilder.setMaxConnTotal(config.getMaxConnections());
    }/*from   w w w.  j  a v a  2 s  . c om*/

    // max connections per route
    if (config.isSetMaxConnectionsPerRoute()) {
        clientBuilder.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
    }

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH)
            .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true);

    // connection timeout
    if (config.isSetConnectionTimeout()) {
        requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout());
    }

    // soucket timeout
    if (config.isSetSocketTimeout()) {
        requestConfigBuilder.setSocketTimeout(config.getSocketTimeout());
    }

    // soucket timeout
    if (config.isSetFollowRedirects()) {
        requestConfigBuilder.setRedirectsEnabled(config.getFollowRedirects());
    }
    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    if (config.isSetUseRetry()) {
        if (config.getUseRetry()) {
            clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
        } else {
            clientBuilder.setRetryHandler(NO_RETRY);
        }
    }

    // basic authentication
    if (config.isSetBasicAuthUsername() && config.isSetBasicAuthPassword()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(config.getBasicAuthUsername(), config.getBasicAuthPassword()));
    }

    if (config.isSetAllowCompression()) {
        clientBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
        clientBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
    }

    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    SSLContext sslcontext = SSLContexts.createSystemDefault();
    // Use custom hostname verifier to customize SSL hostname verification.
    X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build();

    clientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));

    return clientBuilder;
}

From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java

/**
 * Because the creation of the SSLConnectionSocketFactory is expensive (reads from disk
 * the certs file) we will cache the client. The client is re-used so doesn't have any
 * target RTC server info associated with it.
 * @return an HttpClient//from w  w  w.j a  va 2s . c om
 * @throws GeneralSecurityException
 */
private synchronized static CloseableHttpClient getClient() throws GeneralSecurityException {
    if (HTTP_CLIENT == null) {
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();

        clientBuilder.setSSLSocketFactory(getSSLConnectionSocketFactory());

        // TODO to find out if this is sufficient, we can periodically dump the PoolStats
        clientBuilder.setMaxConnPerRoute(10);
        clientBuilder.setMaxConnTotal(100);

        // allow redirects on POST
        clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());

        // How long to wait to get a connection from our connection manager. The default
        // timeouts are forever which is probably not good. 
        // TODO If we set it when creating the GET, PUT & POST maybe its not really needed here
        RequestConfig config = getRequestConfig(CONNECTION_REQUEST_TIMEOUT);
        clientBuilder.setDefaultRequestConfig(config);

        // This will create a client with the defaults (which includes the PoolingConnectionManager)
        HTTP_CLIENT = clientBuilder.build();
    }

    return HTTP_CLIENT;
}

From source file:org.glowroot.central.SyntheticMonitorService.java

private static SyncHttpClientHolder createSyncHttpClientHolder(String shortVersion,
        HttpProxyConfig httpProxyConfig) {
    HttpClientBuilder builder = HttpClients.custom().setUserAgent("GlowrootCentral" + shortVersion)
            .setDefaultHeaders(Arrays.asList(new BasicHeader("Glowroot-Transaction-Type", "Synthetic")));
    if (!httpProxyConfig.host().isEmpty()) {
        int proxyPort = MoreObjects.firstNonNull(httpProxyConfig.port(), 80);
        builder.setProxy(new HttpHost(httpProxyConfig.host(), proxyPort));
    }/*from  w  w w.j  av a 2 s  . c o  m*/
    CloseableHttpClient httpClient = builder.setMaxConnPerRoute(10) // increasing from default 2
            .setMaxConnTotal(1000) // increasing from default 20
            .build();
    return ImmutableSyncHttpClientHolder.of(httpClient, httpProxyConfig);
}

From source file:org.apache.commons.jcs.auxiliary.remote.http.client.AbstractHttpClient.java

/**
 * Configures the http client./*w ww.  jav a  2 s.co  m*/
 *
 * @param builder client builder to configure
 */
protected void configureClient(HttpClientBuilder builder) {
    if (getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0) {
        builder.setMaxConnTotal(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
        builder.setMaxConnPerRoute(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
    }

    builder.setDefaultRequestConfig(RequestConfig.custom()
            .setConnectTimeout(getRemoteHttpCacheAttributes().getConnectionTimeoutMillis())
            .setSocketTimeout(getRemoteHttpCacheAttributes().getSocketTimeoutMillis())
            // By default we instruct HttpClient to ignore cookies.
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build());
}

From source file:org.attribyte.api.http.impl.commons.Commons4Client.java

private void initFromOptions(final ClientOptions options) {

    if (options != ClientOptions.IMPLEMENTATION_DEFAULT) {

        HttpClientBuilder builder = HttpClients.custom();
        builder.setMaxConnTotal(options.maxConnectionsTotal);
        builder.setMaxConnPerRoute(options.maxConnectionsPerDestination);
        builder.setUserAgent(options.userAgent);
        if (options.proxyHost != null) {
            builder.setProxy(new HttpHost(options.proxyHost, options.proxyPort));
        }//from  w  w  w  .  j  a v a2 s  .co  m

        this.defaultRequestConfig = RequestConfig.custom().setConnectTimeout(options.connectionTimeoutMillis)
                .setConnectionRequestTimeout(options.requestTimeoutMillis)
                .setRedirectsEnabled(RequestOptions.DEFAULT_FOLLOW_REDIRECTS)
                .setMaxRedirects(RequestOptions.DEFAULT_FOLLOW_REDIRECTS ? 5 : 0)
                .setAuthenticationEnabled(false).setCircularRedirectsAllowed(false)
                .setSocketTimeout(options.socketTimeoutMillis).build();
        builder.setDefaultRequestConfig(defaultRequestConfig);

        ConnectionConfig connectionConfig = ConnectionConfig.custom()
                .setBufferSize(
                        options.requestBufferSize > options.responseBufferSize ? options.requestBufferSize
                                : options.responseBufferSize)
                .build();
        builder.setDefaultConnectionConfig(connectionConfig);

        this.httpClient = builder.build();
    } else {
        this.defaultRequestConfig = RequestConfig.DEFAULT;
        this.httpClient = HttpClients.createDefault();
    }
}

From source file:com.codeabovelab.dm.gateway.proxy.common.BalancerConfiguration.java

protected CloseableHttpClient configuredHttpClient(HttpClientBuilder httpClientBuilder) {
    LOG.info("HttpClient settings: maxConnections: {}, socketTimeout: {}, connectTimeout: {}", maxConnections,
            socketTimeout, connectTimeout);
    return httpClientBuilder.setMaxConnPerRoute(maxConnections).setMaxConnTotal(maxConnections)
            .setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(socketTimeout)
                    .setConnectTimeout(connectTimeout).build())
            .disableCookieManagement().build();
}

From source file:com.fluidops.fedx.FedX.java

protected FedX(Config config, Cache cache, Statistics statistics, EndpointListProvider endpointListProvider,
        SummaryProvider summaryProvider) {
    this.config = config;
    this.cache = cache;
    this.statistics = statistics;
    this.endpointListProvider = endpointListProvider;
    this.summaryProvider = summaryProvider;

    // initialize httpclient parameters
    HttpClientBuilder httpClientBuilder = HttpClientBuilders.getSSLTrustAllHttpClientBuilder();
    httpClientBuilder.setMaxConnTotal(config.getMaxHttpConnectionCount());
    httpClientBuilder.setMaxConnPerRoute(config.getMaxHttpConnectionCountPerRoute());

    //httpClientBuilder.evictExpiredConnections();
    //httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
    //httpClientBuilder.setConnectionTimeToLive(1000, TimeUnit.MILLISECONDS);
    //httpClientBuilder.disableAutomaticRetries();

    //      httpClientBuilder.setKeepAliveStrategy(new ConnectionKeepAliveStrategy(){
    ///*from w w w  .  j a va2s  .co  m*/
    //          @Override
    //          public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
    //              return 0;
    //          }});

    httpClient = httpClientBuilder.build();

    synchronized (log) {
        if (monitoring == null) {
            monitoring = MonitoringFactory.createMonitoring(config);
        }
    }

    executor = Executors.newCachedThreadPool();

    scheduler = new ControlledWorkerScheduler(config.getWorkerThreads(), "Evaluation Scheduler");
    if (log.isDebugEnabled()) {
        log.debug("Scheduler for async operations initialized with " + config.getWorkerThreads()
                + " worker threads.");
    }

    // initialize prefix declarations, if any
    String prefixFile = config.getPrefixDeclarations();
    if (prefixFile != null) {
        prefixDeclarations = new Properties();
        try {
            prefixDeclarations.load(new FileInputStream(new File(prefixFile)));
        } catch (IOException e) {
            throw new FedXRuntimeException("Error loading prefix properties: " + e.getMessage());
        }
    }
    open = true;
}

From source file:com.ngdata.hbaseindexer.indexer.FusionPipelineClient.java

public FusionPipelineClient(String endpointUrl, String fusionUser, String fusionPass, String fusionRealm)
        throws MalformedURLException {

    this.fusionUser = fusionUser;
    this.fusionPass = fusionPass;
    this.fusionRealm = fusionRealm;

    String fusionLoginConf = System.getProperty(FusionKrb5HttpClientConfigurer.LOGIN_CONFIG_PROP);
    if (fusionLoginConf != null && !fusionLoginConf.isEmpty()) {
        httpClient = FusionKrb5HttpClientConfigurer.createClient(fusionUser);
        isKerberos = true;/* w  ww  .j  a  v a  2 s  .c  o m*/
    } else {
        globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
        cookieStore = new BasicCookieStore();

        // build the HttpClient to be used for all requests
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore);
        httpClientBuilder.setMaxConnPerRoute(100);
        httpClientBuilder.setMaxConnTotal(500);

        if (fusionUser != null && fusionRealm == null)
            httpClientBuilder.addInterceptorFirst(new PreEmptiveBasicAuthenticator(fusionUser, fusionPass));

        httpClient = httpClientBuilder.build();
    }

    originalEndpoints = Arrays.asList(endpointUrl.split(","));
    try {
        sessions = establishSessions(originalEndpoints, fusionUser, fusionPass, fusionRealm);
    } catch (Exception exc) {
        if (exc instanceof RuntimeException) {
            throw (RuntimeException) exc;
        } else {
            throw new RuntimeException(exc);
        }
    }

    random = new Random();
    jsonObjectMapper = new ObjectMapper();

    requestCounter = new AtomicInteger(0);
}

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

public static HttpClientBuilder createHttpClientBuilder(boolean trustSelfSigned, int maxConnections) {

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    httpClientBuilder.disableAutomaticRetries();

    RequestConfig.Builder builder = RequestConfig.custom();

    builder.setConnectTimeout(PropsValues.SYNC_HTTP_CONNECTION_TIMEOUT);

    if (maxConnections == Integer.MAX_VALUE) {
        builder.setSocketTimeout(PropsValues.SYNC_HTTP_SOCKET_TIMEOUT * 2);
    } else {// w  w w .ja va2s  .c  o  m
        builder.setSocketTimeout(PropsValues.SYNC_HTTP_SOCKET_TIMEOUT);
    }

    List<Header> headers = new ArrayList<>(2);

    Header syncBuildHeader = new BasicHeader("Sync-Build", String.valueOf(ReleaseInfo.getBuildNumber()));

    headers.add(syncBuildHeader);

    String syncDeviceType = null;

    if (OSDetector.isApple()) {
        syncDeviceType = "desktop-mac";
    } else if (OSDetector.isLinux()) {
        syncDeviceType = "desktop-linux";
    } else if (OSDetector.isWindows()) {
        syncDeviceType = "desktop-windows";
    }

    Header syncDeviceTypeHeader = new BasicHeader("Sync-Device", syncDeviceType);

    headers.add(syncDeviceTypeHeader);

    httpClientBuilder.setDefaultHeaders(headers);

    httpClientBuilder.setDefaultRequestConfig(builder.build());
    httpClientBuilder.setMaxConnPerRoute(maxConnections);
    httpClientBuilder.setMaxConnTotal(maxConnections);
    httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
    httpClientBuilder.setRoutePlanner(getHttpRoutePlanner());

    try {
        if (trustSelfSigned) {
            httpClientBuilder.setSSLSocketFactory(_getTrustingSSLSocketFactory());
        } else {
            httpClientBuilder.setSSLSocketFactory(_getDefaultSSLSocketFactory());
        }
    } catch (Exception e) {
        _logger.error(e.getMessage(), e);
    }

    return httpClientBuilder;
}

From source file:org.elasticsearch.xpack.watcher.common.http.HttpClient.java

public HttpClient(Settings settings, HttpAuthRegistry httpAuthRegistry, SSLService sslService) {
    super(settings);
    this.httpAuthRegistry = httpAuthRegistry;
    this.defaultConnectionTimeout = HttpSettings.CONNECTION_TIMEOUT.get(settings);
    this.defaultReadTimeout = HttpSettings.READ_TIMEOUT.get(settings);
    this.maxResponseSize = HttpSettings.MAX_HTTP_RESPONSE_SIZE.get(settings);
    this.settingsProxy = getProxyFromSettings();

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // ssl setup/*from ww  w. ja  v a2s  .  c  o m*/
    Settings sslSettings = settings.getByPrefix(SETTINGS_SSL_PREFIX);
    boolean isHostnameVerificationEnabled = sslService.getVerificationMode(sslSettings, Settings.EMPTY)
            .isHostnameVerificationEnabled();
    HostnameVerifier verifier = isHostnameVerificationEnabled ? new DefaultHostnameVerifier()
            : NoopHostnameVerifier.INSTANCE;
    SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(
            sslService.sslSocketFactory(sslSettings), verifier);
    clientBuilder.setSSLSocketFactory(factory);

    clientBuilder.evictExpiredConnections();
    clientBuilder.setMaxConnPerRoute(MAX_CONNECTIONS);
    clientBuilder.setMaxConnTotal(MAX_CONNECTIONS);

    client = clientBuilder.build();
}