List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager setMaxTotal
public void setMaxTotal(final int max)
From source file:com.johnson.grab.browser.HttpClientHolder.java
private static void init() { // custom builder builder = HttpClients.custom();/*from w ww . j ava2s .com*/ PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); manager.setMaxTotal(DEFAULT_MAX_TOTAL_NUM); builder.setConnectionManager(manager); builder.setMaxConnPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); builder.setMaxConnTotal(DEFAULT_MAX_TOTAL_NUM); // headers List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg," + " application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint," + " application/msword, */*")); headers.add(new BasicHeader("Accept-Language", "zh-cn,en-us,zh-tw,en-gb,en;")); headers.add(new BasicHeader("Accept-Charset", "gbk,gb2312,utf-8,BIG5,ISO-8859-1;")); headers.add(new BasicHeader("Connection", "Close")); headers.add(new BasicHeader("Cache-Control", "no-cache")); headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate,sdch")); headers.add(new BasicHeader("Connection", "close")); builder.setDefaultHeaders(headers); // retry handler builder.setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_EXECUTION_NUM) { return false; } if (exception instanceof ConnectTimeoutException) { return false; } if (exception instanceof InterruptedIOException) { return false; } if (exception instanceof UnknownHostException) { return false; } if (exception instanceof NoHttpResponseException) { return false; } if (exception instanceof SSLException) { return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }); // build client client = builder.build(); }
From source file:tv.icntv.common.HttpClientHolder.java
private static void init() { // custom builder builder = HttpClients.custom();//from w w w . j a va2 s . c o m PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); manager.setMaxTotal(DEFAULT_MAX_TOTAL_NUM); builder.setConnectionManager(manager); builder.setMaxConnPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); builder.setMaxConnTotal(DEFAULT_MAX_TOTAL_NUM); // headers List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg," + " application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint," + " application/msword, */*")); // headers.add(new BasicHeader("Content-Type","application/xml")); headers.add(new BasicHeader("Accept-Language", "zh-cn,en-us,zh-tw,en-gb,en;")); headers.add(new BasicHeader("Accept-Charset", "gbk,gb2312,utf-8,BIG5,ISO-8859-1;")); headers.add(new BasicHeader("Connection", "Close")); headers.add(new BasicHeader("Cache-Control", "no-cache")); headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate,sdch")); headers.add(new BasicHeader("Connection", "close")); builder.setDefaultHeaders(headers); // retry handler builder.setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_EXECUTION_NUM) { return false; } if (exception instanceof ConnectTimeoutException) { return false; } if (exception instanceof InterruptedIOException) { return false; } if (exception instanceof UnknownHostException) { return false; } if (exception instanceof NoHttpResponseException) { return false; } if (exception instanceof SSLException) { return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }); // build client client = builder.build(); }
From source file:ee.ria.xroad.common.opmonitoring.OpMonitoringDaemonHttpClient.java
/** * Creates HTTP client./*from ww w.ja v a2s .co m*/ * @param authKey the client's authentication key * @param clientMaxTotalConnections client max total connections * @param clientMaxConnectionsPerRoute client max connections per route * @param connectionTimeoutMilliseconds connection timeout in milliseconds * @param socketTimeoutMilliseconds socket timeout in milliseconds * @return HTTP client * @throws Exception if creating a HTTPS client and SSLContext * initialization fails */ public static CloseableHttpClient createHttpClient(InternalSSLKey authKey, int clientMaxTotalConnections, int clientMaxConnectionsPerRoute, int connectionTimeoutMilliseconds, int socketTimeoutMilliseconds) throws Exception { log.trace("createHttpClient()"); RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create(); if ("https".equalsIgnoreCase(OpMonitoringSystemProperties.getOpMonitorDaemonScheme())) { sfr.register("https", createSSLSocketFactory(authKey)); } else { sfr.register("http", PlainConnectionSocketFactory.INSTANCE); } PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(sfr.build()); cm.setMaxTotal(clientMaxTotalConnections); cm.setDefaultMaxPerRoute(clientMaxConnectionsPerRoute); cm.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); RequestConfig.Builder rb = RequestConfig.custom().setConnectTimeout(connectionTimeoutMilliseconds) .setConnectionRequestTimeout(connectionTimeoutMilliseconds) .setSocketTimeout(socketTimeoutMilliseconds); HttpClientBuilder cb = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(rb.build()); // Disable request retry cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return cb.build(); }
From source file:org.tinymediamanager.scraper.util.TmmHttpClient.java
/** * instantiates a new CloseableHttpClient * //w w w . java 2s . c o m * @return CloseableHttpClient */ public static CloseableHttpClient createHttpClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); // Increase default max connection per route to 5 connectionManager.setMaxTotal(5); HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); httpClientBuilder.setConnectionManager(connectionManager); // my own retry handler HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; httpClientBuilder.setRetryHandler(myRetryHandler); // set proxy if needed if ((Globals.settings.useProxy())) { setProxy(httpClientBuilder); } return httpClientBuilder.build(); }
From source file:io.fabric8.maven.docker.access.hc.http.HttpClientBuilder.java
private static HttpClientConnectionManager getPooledConnectionFactory(String certPath, int maxConnections) throws IOException { PoolingHttpClientConnectionManager ret = certPath != null ? new PoolingHttpClientConnectionManager(getSslFactoryRegistry(certPath)) : new PoolingHttpClientConnectionManager(); ret.setDefaultMaxPerRoute(maxConnections); ret.setMaxTotal(maxConnections); return ret;//from ww w.j av a 2 s. co m }
From source file:org.ops4j.pax.url.mvn.internal.HttpClients.java
private static PoolingHttpClientConnectionManager createConnManager(PropertyResolver resolver, String pid) { boolean SSL_INSECURE = getBoolean(resolver, "maven.wagon.http.ssl.insecure", !getBoolean(resolver, pid + "certificateCheck", false)); boolean IGNORE_SSL_VALIDITY_DATES = getBoolean(resolver, "maven.wagon.http.ssl.ignore.validity.dates", false);//from w w w .j a va2s. c om boolean SSL_ALLOW_ALL = getBoolean(resolver, "maven.wagon.http.ssl.allowall", !getBoolean(resolver, pid + "certificateCheck", false)); boolean PERSISTENT_POOL = getBoolean(resolver, "maven.wagon.http.pool", true); int MAX_CONN_PER_ROUTE = getInteger(resolver, "maven.wagon.httpconnectionManager.maxPerRoute", 20); int MAX_CONN_TOTAL = getInteger(resolver, "maven.wagon.httpconnectionManager.maxTotal", 40); String sslProtocolsStr = getProperty(resolver, "https.protocols", null); String cipherSuitesStr = getProperty(resolver, "https.cipherSuites", null); String[] sslProtocols = sslProtocolsStr != null ? sslProtocolsStr.split(" *, *") : null; String[] cipherSuites = cipherSuitesStr != null ? cipherSuitesStr.split(" *, *") : null; SSLConnectionSocketFactory sslConnectionSocketFactory; if (SSL_INSECURE) { try { SSLContext sslContext = new SSLContextBuilder().useSSL() .loadTrustMaterial(null, new RelaxedTrustStrategy(IGNORE_SSL_VALIDITY_DATES)).build(); sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, sslProtocols, cipherSuites, SSL_ALLOW_ALL ? SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER : SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); } catch (Exception ex) { throw new SSLInitializationException(ex.getMessage(), ex); } } else { sslConnectionSocketFactory = new SSLConnectionSocketFactory( HttpsURLConnection.getDefaultSSLSocketFactory(), sslProtocols, cipherSuites, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); } Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", sslConnectionSocketFactory).build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry); if (PERSISTENT_POOL) { connManager.setDefaultMaxPerRoute(MAX_CONN_PER_ROUTE); connManager.setMaxTotal(MAX_CONN_TOTAL); } else { connManager.setMaxTotal(1); } boolean soKeepAlive = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_KEEPALIVE, false); int soLinger = getInteger(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_LINGER, -1); boolean soReuseAddress = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_REUSEADDRESS, false); boolean soTcpNoDelay = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_TCP_NODELAY, true); // int soTimeout = getInteger( resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_TIMEOUT, 0 ); SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(soKeepAlive) // default false .setSoLinger(soLinger) // default -1 .setSoReuseAddress(soReuseAddress) // default false .setTcpNoDelay(soTcpNoDelay) // default true .setSoTimeout(0) // default 0, but set in org.apache.http.impl.conn.CPoolProxy.setSocketTimeout() // this value is not used .build(); connManager.setDefaultSocketConfig(socketConfig); int bufferSize = getInteger(resolver, pid + ServiceConstants.PROPERTY_CONNECTION_BUFFER_SIZE, 8192); ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(bufferSize) // default 8192 .setFragmentSizeHint(bufferSize) // default 'buffer size' .build(); connManager.setDefaultConnectionConfig(connectionConfig); return connManager; }
From source file:ee.ria.xroad.common.request.ManagementRequestClient.java
private static CloseableHttpClient createHttpClient(KeyManager km, TrustManager tm) throws Exception { RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create(); sfr.register("http", PlainConnectionSocketFactory.INSTANCE); SSLContext ctx = SSLContext.getInstance(CryptoUtils.SSL_PROTOCOL); ctx.init(km != null ? new KeyManager[] { km } : null, tm != null ? new TrustManager[] { tm } : null, new SecureRandom()); SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); sfr.register("https", sf); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(sfr.build()); cm.setMaxTotal(CLIENT_MAX_TOTAL_CONNECTIONS); cm.setDefaultMaxPerRoute(CLIENT_MAX_CONNECTIONS_PER_ROUTE); int timeout = SystemProperties.getClientProxyTimeout(); int socketTimeout = SystemProperties.getClientProxyHttpClientTimeout(); RequestConfig.Builder rb = RequestConfig.custom(); rb.setConnectTimeout(timeout);/*from w w w.j a v a 2s . c o m*/ rb.setConnectionRequestTimeout(timeout); rb.setSocketTimeout(socketTimeout); HttpClientBuilder cb = HttpClients.custom(); cb.setConnectionManager(cm); cb.setDefaultRequestConfig(rb.build()); // Disable request retry cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return cb.build(); }
From source file:com.moviejukebox.tools.YamjHttpClientBuilder.java
@SuppressWarnings("resource") private static YamjHttpClient buildHttpClient() { LOG.trace("Create new YAMJ http client"); // create proxy HttpHost proxy = null;// ww w .j ava2 s . c om CredentialsProvider credentialsProvider = null; if (StringUtils.isNotBlank(PROXY_HOST) && PROXY_PORT > 0) { proxy = new HttpHost(PROXY_HOST, PROXY_PORT); if (StringUtils.isNotBlank(PROXY_USERNAME) && StringUtils.isNotBlank(PROXY_PASSWORD)) { credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT), new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD)); } } PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT_SOCKET).build()); connManager.setMaxTotal(20); connManager.setDefaultMaxPerRoute(2); CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000).setMaxObjectSize(8192).build(); HttpClientBuilder builder = CachingHttpClientBuilder.create().setCacheConfig(cacheConfig) .setConnectionManager(connManager).setProxy(proxy) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT_READ) .setConnectTimeout(TIMEOUT_CONNECT).setSocketTimeout(TIMEOUT_SOCKET) .setCookieSpec(CookieSpecs.IGNORE_COOKIES).setProxy(proxy).build()); // show status showStatus(); // build the client YamjHttpClient wrapper = new YamjHttpClient(builder.build(), connManager); wrapper.setUserAgentSelector(new WebBrowserUserAgentSelector()); wrapper.addGroupLimit(".*", 1); // default limit, can be overwritten // First we have to read/create the rules String maxDownloadSlots = PropertiesUtil.getProperty("mjb.MaxDownloadSlots"); if (StringUtils.isNotBlank(maxDownloadSlots)) { LOG.debug("Using download limits: {}", maxDownloadSlots); Pattern pattern = Pattern.compile(",?\\s*([^=]+)=(\\d+)"); Matcher matcher = pattern.matcher(maxDownloadSlots); while (matcher.find()) { String group = matcher.group(1); try { final Integer maxResults = Integer.valueOf(matcher.group(2)); wrapper.addGroupLimit(group, maxResults); LOG.trace("Added download slot '{}' with max results {}", group, maxResults); } catch (NumberFormatException error) { LOG.debug("Rule '{}' is no valid regexp, ignored", group); } } } return wrapper; }
From source file:org.apache.metron.dataloads.taxii.TaxiiHandler.java
private static HttpClient buildClient(URL proxy, String username, String password) throws Exception { HttpClient client = new HttpClient(); // Start with a default TAXII HTTP client. // Create an Apache HttpClientBuilder to be customized by the command line arguments. HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties(); // Proxy/* www. j ava2 s. c o m*/ if (proxy != null) { HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol()); builder.setProxy(proxyHost); } // Basic authentication. User & Password if (username != null ^ password != null) { throw new Exception("'username' and 'password' arguments are required to appear together."); } // from: http://stackoverflow.com/questions/19517538/ignoring-ssl-certificate-in-apache-httpclient-4-3 SSLContextBuilder ssbldr = new SSLContextBuilder(); ssbldr.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ssbldr.build(), SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry); cm.setMaxTotal(20);//max connection System.setProperty("jsse.enableSNIExtension", "false"); //"" CloseableHttpClient httpClient = builder.setSSLSocketFactory(sslsf).setConnectionManager(cm).build(); client.setHttpclient(httpClient); return client; }
From source file:com.pearson.pdn.demos.chainoflearning.EventingUtility.java
static void init() { if (client == null) { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(50);/*w ww .j a va 2 s . c om*/ cm.setMaxTotal(100); client = HttpClientBuilder.create().setConnectionManager(cm).build(); } }