List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager setMaxTotal
public void setMaxTotal(final int max)
From source file:com.twinsoft.convertigo.engine.util.HttpUtils.java
@SuppressWarnings("deprecation") public static CloseableHttpClient makeHttpClient4(boolean usePool) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setDefaultRequestConfig( RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build()); if (usePool) { PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); int maxTotalConnections = 100; try {/*from www . j a v a 2s .co m*/ maxTotalConnections = new Integer( EnginePropertiesManager.getProperty(PropertyName.HTTP_CLIENT_MAX_TOTAL_CONNECTIONS)) .intValue(); } catch (NumberFormatException e) { Engine.logEngine.warn("Unable to retrieve the max number of connections; defaults to 100."); } int maxConnectionsPerHost = 50; try { maxConnectionsPerHost = new Integer( EnginePropertiesManager.getProperty(PropertyName.HTTP_CLIENT_MAX_CONNECTIONS_PER_HOST)) .intValue(); } catch (NumberFormatException e) { Engine.logEngine .warn("Unable to retrieve the max number of connections per host; defaults to 100."); } connManager.setDefaultMaxPerRoute(maxConnectionsPerHost); connManager.setMaxTotal(maxTotalConnections); httpClientBuilder.setConnectionManager(connManager); } return httpClientBuilder.build(); }
From source file:org.nebula.framework.client.NebulaRestClient.java
public NebulaRestClient(String accessId, String secretKey, String hostname, int port, String contextPath, int connectionTimeoutInSecs, int socketTimeoutInSecs, int maxTotalConnections) { this.accessId = accessId; this.secretKey = secretKey; this.target = new HttpHost(hostname, port); requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionTimeoutInSecs * 1000) .setConnectTimeout(connectionTimeoutInSecs * 1000).setSocketTimeout(socketTimeoutInSecs * 1000) .build();// w ww . ja v a 2 s . c o m requestMapper = new RequestMapper(contextPath); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(maxTotalConnections); cm.setDefaultMaxPerRoute(maxTotalConnections); client = HttpClientBuilder.create().setConnectionManager(cm).build(); }
From source file:au.com.borner.salesforce.client.rest.ConnectionManager.java
public ConnectionManager() { loggedIn = false;/* ww w . j a v a2s. c om*/ SSLContext sslContext = SSLContexts.createSystemDefault(); LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext); PoolingHttpClientConnectionManager pcm = new PoolingHttpClientConnectionManager(); pcm.setMaxTotal(100); pcm.setDefaultMaxPerRoute(50); HttpCompressionRequestInterceptor requestInterceptor = new HttpCompressionRequestInterceptor(); HttpCompressionResponseInterceptor responseInterceptor = new HttpCompressionResponseInterceptor(); httpClient = HttpClients.custom().setConnectionManager(pcm).setSSLSocketFactory(sslSocketFactory) .addInterceptorFirst(requestInterceptor).addInterceptorFirst(responseInterceptor).build(); }
From source file:com.kugou.opentsdb.OpenTsdb.java
private OpenTsdb(String hostname, int port, int connectionTimeout, int connectionRequestTimeout, int batchSizeLimit) { RequestConfig config = RequestConfig.custom().setConnectTimeout(connectionTimeout) .setConnectionRequestTimeout(connectionRequestTimeout).setCookieSpec(CookieSpecs.IGNORE_COOKIES) .build();/* w w w . j a v a2 s . com*/ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE); cm.setMaxTotal(MAX_CONNECTIONS_TOTAL); client = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(config).build(); host = new HttpHost(hostname, port); this.batchSizeLimit = batchSizeLimit; this.connectionTimeout = connectionTimeout; this.connectionRequestTimeout = connectionRequestTimeout; }
From source file:monasca.persister.repository.influxdb.InfluxV9RepoWriter.java
@Inject public InfluxV9RepoWriter(final PersisterConfig config) { this.influxName = config.getInfluxDBConfiguration().getName(); this.influxUrl = config.getInfluxDBConfiguration().getUrl() + "/write"; this.influxUser = config.getInfluxDBConfiguration().getUser(); this.influxPass = config.getInfluxDBConfiguration().getPassword(); this.influxCreds = this.influxUser + ":" + this.influxPass; this.influxRetentionPolicy = config.getInfluxDBConfiguration().getRetentionPolicy(); this.gzip = config.getInfluxDBConfiguration().getGzip(); this.baseAuthHeader = "Basic " + new String(Base64.encodeBase64(this.influxCreds.getBytes())); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(config.getInfluxDBConfiguration().getMaxHttpConnections()); if (this.gzip) { this.httpClient = HttpClients.custom().setConnectionManager(cm) .addInterceptorFirst(new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); }/* w w w. j a v a 2 s . c o m*/ } }).addInterceptorFirst(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; } } } } } }).build(); } else { this.httpClient = HttpClients.custom().setConnectionManager(cm).build(); } }
From source file:com.github.horrorho.liquiddonkey.http.HttpClientFactory.java
public CloseableHttpClient client() { logger.trace("<< client()"); PoolingHttpClientConnectionManager connectionManager = config.isRelaxedSSL() ? new PoolingHttpClientConnectionManager(relaxedSocketFactoryRegistry()) : new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(config.maxConnections()); connectionManager.setDefaultMaxPerRoute(config.maxConnections()); connectionManager.setValidateAfterInactivity(config.validateAfterInactivityMs()); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(config.timeoutMs()) .setConnectTimeout(config.timeoutMs()).setSocketTimeout(config.timeoutMs()).build(); HttpRequestRetryHandler httpRequestRetryHandler = config.isPersistent() ? new PersistentHttpRequestRetryHandler(config.retryCount(), config.retryDelayMs(), config.timeoutMs(), true) : new DefaultHttpRequestRetryHandler(config.retryCount(), false); CloseableHttpClient client = HttpClients.custom().setRetryHandler(httpRequestRetryHandler) .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig) .setUserAgent(config.userAgent()).build(); logger.trace(">> client()", client); return client; }
From source file:com.continuuity.loom.scheduler.callback.HttpPostClusterCallback.java
public void initialize(Configuration conf, ClusterStoreService clusterStoreService) { this.clusterStoreService = clusterStoreService; this.onStartUrl = conf.get(Constants.HttpCallback.START_URL); this.onSuccessUrl = conf.get(Constants.HttpCallback.SUCCESS_URL); this.onFailureUrl = conf.get(Constants.HttpCallback.FAILURE_URL); this.startTriggerActions = parseActionsString( conf.get(Constants.HttpCallback.START_TRIGGERS, Constants.HttpCallback.DEFAULT_START_TRIGGERS)); this.successTriggerActions = parseActionsString( conf.get(Constants.HttpCallback.SUCCESS_TRIGGERS, Constants.HttpCallback.DEFAULT_SUCCESS_TRIGGERS)); this.failureTriggerActions = parseActionsString( conf.get(Constants.HttpCallback.FAILURE_TRIGGERS, Constants.HttpCallback.DEFAULT_FAILURE_TRIGGERS)); if (onStartUrl != null) { LOG.debug("before hook will be triggered on actions {}", Joiner.on(',').join(startTriggerActions)); }//from ww w.j a va2 s . co m if (onSuccessUrl != null) { LOG.debug("after hook will be triggered on actions {}", Joiner.on(',').join(successTriggerActions)); } if (onFailureUrl != null) { LOG.debug("after hook will be triggered on actions {}", Joiner.on(',').join(failureTriggerActions)); } int maxConnections = conf.getInt(Constants.HttpCallback.MAX_CONNECTIONS, Constants.HttpCallback.DEFAULT_MAX_CONNECTIONS); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(maxConnections); connectionManager.setMaxTotal(maxConnections); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout( conf.getInt(Constants.HttpCallback.SOCKET_TIMEOUT, Constants.HttpCallback.DEFAULT_SOCKET_TIMEOUT)) .build(); connectionManager.setDefaultSocketConfig(socketConfig); this.httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build(); }
From source file:leap.lang.http.client.apache.ApacheHttpClient.java
protected CloseableHttpClient initHttpClient() { HttpClientBuilder cb = HttpClientBuilder.create(); //TODO : small buffer size will cause socket closed when reading response entity? PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(getDefaultRegistry()); //cm.setDefaultConnectionConfig(ConnectionConfig.custom().setBufferSize(1024 * 1024).build()); cm.setMaxTotal(maxConnectionTotal); cm.setDefaultMaxPerRoute(maxConnectionPerRoute); if (bufferSize > 0) { ConnectionConfig cc = ConnectionConfig.copy(ConnectionConfig.DEFAULT).setBufferSize(bufferSize).build(); cm.setDefaultConnectionConfig(cc); }/*from w w w .ja va2s .co m*/ cb.setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); cb.setConnectionManager(cm); cb.setDefaultRequestConfig(requestConfig); return cb.build(); }
From source file:org.openhab.binding.ihc.ws.IhcConnectionPool.java
private void init() { // Create a local instance of cookie store cookieStore = new BasicCookieStore(); // Create local HTTP context localContext = HttpClientContext.create(); // Bind custom cookie store to the local context localContext.setCookieStore(cookieStore); httpClientBuilder = HttpClientBuilder.create(); // Setup a Trust Strategy that allows all certificates. logger.debug("Initialize SSL context"); // Create a trust manager that does not validate certificate chains, // but accept all. TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override/*from w ww . j a va 2 s . com*/ public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { logger.trace("Trusting server cert: " + certs[0].getIssuerDN()); } } }; // Install the all-trusting trust manager try { // Controller supports only SSLv3 and TLSv1 sslContext = SSLContext.getInstance("TLSv1"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { logger.warn("Exception", e); } catch (KeyManagementException e) { logger.warn("Exception", e); } httpClientBuilder.setSslcontext(sslContext); // Controller accepts only HTTPS connections and because normally IP // address are used on home network rather than DNS names, create custom // host name verifier. HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { logger.trace("HostnameVerifier: arg0 = " + arg0); logger.trace("HostnameVerifier: arg1 = " + arg1); return true; } }; // Create an SSL Socket Factory, to use our weakened "trust strategy" SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslSocketFactory).build(); // Create connection-manager using our Registry. Allows multi-threaded // use PoolingHttpClientConnectionManager connMngr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); // Increase max connection counts connMngr.setMaxTotal(20); connMngr.setDefaultMaxPerRoute(6); httpClientBuilder.setConnectionManager(connMngr); }