Java Code Examples for org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setMaxPerRoute()
The following examples show how to use
org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setMaxPerRoute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpProtocolParent.java From dubbox with Apache License 2.0 | 6 votes |
private CloseableHttpClient createHttpClient(String hostname, int port) { ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory(); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", plainsf).register("https", sslsf).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry); // 将最大连接数增加 cm.setMaxTotal(maxTotal); // 将每个路由基础的连接增加 cm.setDefaultMaxPerRoute(maxPerRoute); HttpHost httpHost = new HttpHost(hostname, port); // 将目标主机的最大连接数增加 cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute); // 请求重试处理 return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build(); }
Example 2
Source File: HttpProtocolParent.java From dtsopensource with Apache License 2.0 | 6 votes |
private CloseableHttpClient createHttpClient(String hostname, int port) { ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory(); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", plainsf).register("https", sslsf).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry); // 将最大连接数增加 cm.setMaxTotal(maxTotal); // 将每个路由基础的连接增加 cm.setDefaultMaxPerRoute(maxPerRoute); HttpHost httpHost = new HttpHost(hostname, port); // 将目标主机的最大连接数增加 cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute); // 请求重试处理 return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build(); }
Example 3
Source File: PingCheckMonitor.java From ranger with Apache License 2.0 | 6 votes |
/** * @param timeEntity how often the {@link #monitor()} check needs to be executed * @param httpRequest http request that will be called at regular intervals * @param pingTimeoutInMilliseconds timeout in milliseconds for http request execution (ping response) * @param pingWindowSize rolling window frame, which needs to be maintained * @param maxFailures maximum failures allowed in the rolling window frame * @param host host name (could be localhost) * @param port port */ public PingCheckMonitor(TimeEntity timeEntity, HttpRequest httpRequest, Integer pingTimeoutInMilliseconds, Integer pingWindowSize, Integer maxFailures, String host, Integer port) { super(PingCheckMonitor.class.getSimpleName(), timeEntity); this.httpRequest = httpRequest; this.pingTimeoutInMilliseconds = pingTimeoutInMilliseconds; this.host = host; this.port = port; this.rollingWindowHealthQueue = new RollingWindowHealthQueue(pingWindowSize, maxFailures); this.executorService = Executors.newSingleThreadExecutor(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(host, port)), 2); this.httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .build(); }
Example 4
Source File: RestClient.java From light with Apache License 2.0 | 6 votes |
private CloseableHttpClient httpClient() throws Exception { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry()); Map<String, Object> httpClientMap = (Map<String, Object>)configMap.get(REST_TEMPLATE); connectionManager.setMaxTotal((Integer)httpClientMap.get(MAX_CONNECTION_TOTAL)); connectionManager.setDefaultMaxPerRoute((Integer) httpClientMap.get(MAX_CONNECTION_PER_ROUTE)); // Now handle all the specific route defined. Map<String, Object> routeMap = (Map<String, Object>)httpClientMap.get(ROUTES); Iterator<String> it = routeMap.keySet().iterator(); while (it.hasNext()) { String route = it.next(); Integer maxConnection = (Integer)routeMap.get(route); connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost( route)), maxConnection); } RequestConfig config = RequestConfig.custom() .setConnectTimeout((Integer)httpClientMap.get(TIMEOUT_MILLISECONDS)) .build(); return HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultRequestConfig(config).build(); }
Example 5
Source File: HttpClientConnectionManagementLiveTest.java From tutorials with MIT License | 5 votes |
@Test // Example 4.1 public final void whenIncreasingConnectionPool_thenNoEceptions() { poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager.setMaxTotal(5); poolingConnManager.setDefaultMaxPerRoute(4); final HttpHost localhost = new HttpHost("locahost", 80); poolingConnManager.setMaxPerRoute(new HttpRoute(localhost), 5); }
Example 6
Source File: SimpleHttpClientFactoryBean.java From springboot-shiro-cas-mybatis with MIT License | 4 votes |
/** * Build a HTTP client based on the current properties. * * @return the built HTTP client */ private CloseableHttpClient buildHttpClient() { try { final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory; final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", plainsf) .register("https", sslsf) .build(); final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry); connMgmr.setMaxTotal(this.maxPooledConnections); connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute); final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost()); final HttpRoute httpRoute = new HttpRoute(httpHost); connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE); final RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(this.readTimeout) .setConnectTimeout(this.connectionTimeout) .setConnectionRequestTimeout(this.connectionTimeout) .setStaleConnectionCheckEnabled(true) .setCircularRedirectsAllowed(this.circularRedirectsAllowed) .setRedirectsEnabled(this.redirectsEnabled) .setAuthenticationEnabled(this.authenticationEnabled) .build(); final HttpClientBuilder builder = HttpClients.custom() .setConnectionManager(connMgmr) .setDefaultRequestConfig(requestConfig) .setSSLSocketFactory(sslsf) .setSSLHostnameVerifier(this.hostnameVerifier) .setRedirectStrategy(this.redirectionStrategy) .setDefaultCredentialsProvider(this.credentialsProvider) .setDefaultCookieStore(this.cookieStore) .setConnectionReuseStrategy(this.connectionReuseStrategy) .setConnectionBackoffStrategy(this.connectionBackoffStrategy) .setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy) .setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy) .setDefaultHeaders(this.defaultHeaders) .useSystemProperties(); return builder.build(); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); throw new RuntimeException(e); } }
Example 7
Source File: ApacheHttpClient.java From jiguang-java-client-common with MIT License | 4 votes |
public CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) { ConnectionSocketFactory plainsf = PlainConnectionSocketFactory .getSocketFactory(); LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory .getSocketFactory(); Registry<ConnectionSocketFactory> registry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", plainsf) .register("https", sslsf).build(); _cm = new PoolingHttpClientConnectionManager( registry); // 将最大连接数增加 _cm.setMaxTotal(maxTotal); // 将每个路由基础的连接增加 _cm.setDefaultMaxPerRoute(maxPerRoute); HttpHost httpHost = new HttpHost(hostname, port); // 将目标主机的最大连接数增加 _cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute); // 请求重试处理 HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= _maxRetryTimes) { return false; } if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试 return true; } if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常 return false; } if (exception instanceof InterruptedIOException) {// 超时 return false; } if (exception instanceof UnknownHostException) {// 目标服务器不可达 return false; } if (exception instanceof ConnectTimeoutException) {// 连接被拒绝 return false; } if (exception instanceof SSLException) {// SSL握手异常 return false; } HttpClientContext clientContext = HttpClientContext .adapt(context); HttpRequest request = clientContext.getRequest(); // 如果请求是幂等的,就再次尝试 if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(_cm) .setRetryHandler(httpRequestRetryHandler).build(); return httpClient; }