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

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

Introduction

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

Prototype

public final HttpClientBuilder setMaxConnTotal(final int maxConnTotal) 

Source Link

Document

Assigns maximum total connection 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());
    }/*ww  w.j a v a2s .com*/

    // 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//w  w  w  . j  a v  a  2s .  com
 * @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.apache.commons.jcs.auxiliary.remote.http.client.AbstractHttpClient.java

/**
 * Configures the http client.//from  w ww. j a  va 2s  .c o 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));
        }// ww w. j  ava 2 s  .  c o 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.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 ww w . java 2  s . c  o  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 w w .j  a va  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 {/*from w  w w  .  ja  v a 2 s .  c  om*/
        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   w  ww  .  j av  a 2 s. com
    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();
}

From source file:org.ow2.proactive.http.CommonHttpClientBuilder.java

public CloseableHttpClient build() {
    org.apache.http.impl.client.HttpClientBuilder internalHttpClientBuilder = createInternalHttpClientBuilder();

    if (useSystemProperties) {
        internalHttpClientBuilder.useSystemProperties();
    }/*from   www  .j a  va 2  s  .co m*/

    if (overrideAllowAnyHostname != null) {
        if (overrideAllowAnyHostname) {
            acceptAnyHostname = true;
        } else {
            acceptAnyHostname = false;
        }
    }

    if (overrideAllowAnyCertificate != null) {
        if (overrideAllowAnyCertificate) {
            acceptAnyCertificate = true;
        } else {
            acceptAnyCertificate = false;
        }
    }

    if (acceptAnyCertificate) {
        internalHttpClientBuilder.setSslcontext(createSslContext());
    }

    if (acceptAnyHostname) {
        internalHttpClientBuilder.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }

    if (maxConnections > 0) {
        internalHttpClientBuilder.setMaxConnPerRoute(maxConnections);
        internalHttpClientBuilder.setMaxConnTotal(maxConnections);
    }

    if (requestConfig != null) {
        internalHttpClientBuilder.setDefaultRequestConfig(requestConfig);
    }

    if (!useContentCompression) {
        internalHttpClientBuilder.disableContentCompression();
    }

    return internalHttpClientBuilder.build();
}

From source file:com.nextdoor.bender.ipc.http.AbstractHttpTransportFactory.java

protected HttpClientBuilder getClientBuilder(boolean useSSL, String url, Map<String, String> stringHeaders,
        int socketTimeout) {

    HttpClientBuilder cb = HttpClientBuilder.create();

    /*//w w  w  .  j a  v a  2 s  . c  om
     * Setup SSL
     */
    if (useSSL) {
        /*
         * All trusting SSL context
         */
        try {
            cb.setSSLContext(getSSLContext());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        /*
         * All trusting hostname verifier
         */
        cb.setSSLHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
    }

    /*
     * Add default headers
     */
    ArrayList<BasicHeader> headers = new ArrayList<BasicHeader>(stringHeaders.size());
    stringHeaders.forEach((k, v) -> headers.add(new BasicHeader(k, v)));
    cb.setDefaultHeaders(headers);

    /*
     * Set socket timeout and transport threads
     */
    SocketConfig sc = SocketConfig.custom().setSoTimeout(socketTimeout).build();
    cb.setDefaultSocketConfig(sc);
    cb.setMaxConnPerRoute(this.config.getThreads());
    cb.setMaxConnTotal(this.config.getThreads());

    return cb;
}