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

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

Introduction

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

Prototype

public void setMaxTotal(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   ww w.  ja v a 2 s . c  o  m*/
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.microsoft.cognitive.speakerrecognition.SpeakerIdentificationRestClient.java

/**
 * Initializes an instance of the service client
 *
 * @param subscriptionKey The subscription key to use
 *//*from w w w.java 2s .c  o m*/
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:org.exoplatform.outlook.mail.MailAPI.java

/**
 * Instantiates a new mail API./*from   w  ww  .ja  va2 s  .c  om*/
 *
 * @param httpClient the http client
 * @throws MailServerException the mail server exception
 */
MailAPI(CloseableHttpClient httpClient) throws MailServerException {

    if (httpClient == null) {
        // FYI it's possible make more advanced conn manager settings (host verification X509, conn config,
        // message parser etc.)
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        // 2 recommended by RFC 2616 sec 8.1.4, we make it bigger for quicker // upload
        connectionManager.setDefaultMaxPerRoute(10);
        connectionManager.setMaxTotal(100);

        // Create global request configuration
        RequestConfig defaultRequestConfig = RequestConfig.custom().setExpectContinueEnabled(true)
                .setStaleConnectionCheckEnabled(true).setAuthenticationEnabled(true)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                // .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                // .setCookieSpec(CookieSpecs.BEST_MATCH)
                .build();

        // Create HTTP client
        this.httpClient = HttpClients.custom().setConnectionManager(connectionManager)
                // .setDefaultCredentialsProvider(credsProvider)
                .setDefaultRequestConfig(defaultRequestConfig).build();
    } else {
        // Use given HTTP client (for tests)
        this.httpClient = httpClient;
    }

    // Default header (Accept JSON), add to those requests where required
    this.acceptJsonHeader = new BasicHeader("Accept", ContentType.APPLICATION_JSON.getMimeType());

    // Add AuthCache to the execution context
    this.httpContext = HttpClientContext.create();
}

From source file:com.normalexception.app.rx8club.html.LoginFactory.java

/**
 * Initialize the client, cookie store, and context
 *///from   ww  w .j av a 2 s.co  m
private void initializeClientInformation() {
    Log.d(TAG, "Initializing Client...");

    /*
    Log.d(TAG, "Creating Custom Cache Configuration");
    CacheConfig cacheConfig = CacheConfig.custom()
      .setMaxCacheEntries(1000)
      .setMaxObjectSize(8192)
      .build();
    */

    Log.d(TAG, "Creating Custom Request Configuration");
    RequestConfig rConfig = RequestConfig.custom().setCircularRedirectsAllowed(true)
            .setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();

    Log.d(TAG, "Building Custom HTTP Client");
    HttpClientBuilder httpclientbuilder = HttpClients.custom();
    //httpclientbuilder.setCacheConfig(cacheConfig);
    httpclientbuilder.setDefaultRequestConfig(rConfig);
    httpclientbuilder.setDefaultCookieStore(cookieStore);

    Log.d(TAG, "Connection Manager Initializing");
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(20);
    httpclientbuilder.setConnectionManager(cm);

    Log.d(TAG, "Enable GZIP Compression");
    httpclientbuilder.addInterceptorLast(new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });
    httpclientbuilder.addInterceptorLast(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }

    });

    // Follow Redirects
    Log.d(TAG, "Registering Redirect Strategy");
    httpclientbuilder.setRedirectStrategy(new RedirectStrategy());

    // Setup retry handler
    Log.d(TAG, "Registering Retry Handler");
    httpclientbuilder.setRetryHandler(new RetryHandler());

    // Setup KAS
    Log.d(TAG, "Registering Keep Alive Strategy");
    httpclientbuilder.setKeepAliveStrategy(new KeepAliveStrategy());

    httpclient = httpclientbuilder.build();

    //httpclient.log.enableDebug(
    //      MainApplication.isHttpClientLogEnabled());

    isInitialized = true;
}

From source file:org.codelibs.solr.lib.server.SolrLibHttpSolrServer.java

public void init() {
    if (clientConnectionManager == null) {
        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost);
        connectionManager.setMaxTotal(maxTotalConnections);
        idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager,
                connectionMonitorInterval, connectionIdelTimeout);
        idleConnectionMonitorThread.start();
        clientConnectionManager = connectionManager;
    }/*  www.j a v  a 2  s  . c o  m*/

    requestConfigBuilder.setRedirectsEnabled(followRedirects);

    final HttpClientBuilder builder = HttpClients.custom();

    if (allowCompression) {
        builder.addInterceptorLast(new UseCompressionRequestInterceptor());
        builder.addInterceptorLast(new UseCompressionResponseInterceptor());
    }

    for (final HttpRequestInterceptor iterceptor : httpRequestInterceptorList) {
        builder.addInterceptorLast(iterceptor);
    }

    init(builder.setConnectionManager(clientConnectionManager)
            .setDefaultRequestConfig(requestConfigBuilder.build()).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.muk.services.configuration.ServiceConfig.java

@Bean
public HttpClientConnectionManager poolingConnectionManager() {
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(200);
    connManager.setDefaultMaxPerRoute(20);

    return connManager;
}

From source file:com.muk.services.configuration.ServiceConfig.java

@Bean
public HttpClientConnectionManager genericConnectionManager() {
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(10);
    connManager.setDefaultMaxPerRoute(4);

    return connManager;
}

From source file:groovyx.net.http.ApacheHttpBuilder.java

/**
 * Creates a new `HttpBuilder` based on the Apache HTTP client. While it is acceptable to create a builder with this method, it is generally
 * preferred to use one of the `static` `configure(...)` methods.
 *
 * @param config the configuration object
 *//* ww  w.ja  va  2 s.c om*/
public ApacheHttpBuilder(final HttpObjectConfig config) {
    super(config);

    this.proxyInfo = config.getExecution().getProxyInfo();
    this.config = new HttpConfigs.ThreadSafeHttpConfig(config.getChainedConfig());
    this.executor = config.getExecution().getExecutor();
    this.clientConfig = config.getClient();

    final HttpClientBuilder myBuilder = HttpClients.custom();

    final Registry<ConnectionSocketFactory> registry = registry(config);

    if (config.getExecution().getMaxThreads() > 1) {
        final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
        cm.setMaxTotal(config.getExecution().getMaxThreads());
        cm.setDefaultMaxPerRoute(config.getExecution().getMaxThreads());
        myBuilder.setConnectionManager(cm);
    } else {
        final BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry);
        myBuilder.setConnectionManager(cm);
    }

    final SSLContext sslContext = config.getExecution().getSslContext();
    if (sslContext != null) {
        myBuilder.setSSLContext(sslContext);
        myBuilder.setSSLSocketFactory(
                new SSLConnectionSocketFactory(sslContext, config.getExecution().getHostnameVerifier()));
    }

    myBuilder.addInterceptorFirst((HttpResponseInterceptor) (response, context) -> {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    final Consumer<Object> clientCustomizer = clientConfig.getClientCustomizer();
    if (clientCustomizer != null) {
        clientCustomizer.accept(myBuilder);
    }

    this.client = myBuilder.build();
}

From source file:com.gooddata.GoodData.java

private HttpClientBuilder createHttpClientBuilder(final GoodDataSettings settings) {
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(settings.getMaxConnections());
    connectionManager.setMaxTotal(settings.getMaxConnections());

    final SocketConfig.Builder socketConfig = SocketConfig.copy(SocketConfig.DEFAULT);
    socketConfig.setSoTimeout(settings.getSocketTimeout());
    connectionManager.setDefaultSocketConfig(socketConfig.build());

    final RequestConfig.Builder requestConfig = RequestConfig.copy(RequestConfig.DEFAULT);
    requestConfig.setConnectTimeout(settings.getConnectionTimeout());
    requestConfig.setConnectionRequestTimeout(settings.getConnectionRequestTimeout());
    requestConfig.setSocketTimeout(settings.getSocketTimeout());

    return HttpClientBuilder.create()
            .setUserAgent(StringUtils.isNotBlank(settings.getUserAgent())
                    ? String.format("%s %s", settings.getUserAgent(), getUserAgent())
                    : getUserAgent())//w  w  w .  j ava2s .  c o m
            .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig.build());
}