Java Code Examples for org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setDefaultSocketConfig()
The following examples show how to use
org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setDefaultSocketConfig() .
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: HttpUtils.java From ScriptSpider with Apache License 2.0 | 6 votes |
/** * 创建httpclient连接池,并初始化httpclient */ public void init() { try { SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()) .build(); HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.getDefaultHostnameVerifier(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslsf) .build(); httpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); // Increase max total connection to 200 httpClientConnectionManager.setMaxTotal(maxTotalPool); // Increase default max connection per route to 20 httpClientConnectionManager.setDefaultMaxPerRoute(maxConPerRoute); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(socketTimeout).build(); httpClientConnectionManager.setDefaultSocketConfig(socketConfig); } catch (Exception e) { } }
Example 2
Source File: SeedManager.java From joal with Apache License 2.0 | 6 votes |
public SeedManager(final String joalConfFolder, final ObjectMapper mapper, final ApplicationEventPublisher publisher) throws IOException { this.isSeeding = false; this.joalFoldersPath = new JoalFoldersPath(Paths.get(joalConfFolder)); this.torrentFileProvider = new TorrentFileProvider(joalFoldersPath); this.configProvider = new JoalConfigProvider(mapper, joalFoldersPath, publisher); this.bitTorrentClientProvider = new BitTorrentClientProvider(configProvider, mapper, joalFoldersPath); this.publisher = publisher; this.connectionHandler = new ConnectionHandler(); final SocketConfig sc = SocketConfig.custom() .setSoTimeout(30000) .build(); final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultMaxPerRoute(100); connManager.setMaxTotal(200); connManager.setValidateAfterInactivity(1000); connManager.setDefaultSocketConfig(sc); this.httpClient = HttpClients.custom() .setConnectionTimeToLive(1, TimeUnit.MINUTES) .setConnectionManager(connManager) .setConnectionManagerShared(true) .build(); }
Example 3
Source File: ApacheHttpClient.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
public HttpClientConnectionManager create(ApacheHttpClient.DefaultBuilder configuration, AttributeMap standardOptions) { ConnectionSocketFactory sslsf = getPreferredSocketFactory(configuration, standardOptions); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager( createSocketFactoryRegistry(sslsf), null, DefaultSchemePortResolver.INSTANCE, null, standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE).toMillis(), TimeUnit.MILLISECONDS); cm.setDefaultMaxPerRoute(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS)); cm.setMaxTotal(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS)); cm.setDefaultSocketConfig(buildSocketConfig(standardOptions)); return cm; }
Example 4
Source File: ApacheConnectionManagerFactory.java From ibm-cos-sdk-java with Apache License 2.0 | 6 votes |
@Override public HttpClientConnectionManager create(final HttpClientSettings settings) { ConnectionSocketFactory sslsf = getPreferredSocketFactory(settings); final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager( createSocketFactoryRegistry(sslsf), null, DefaultSchemePortResolver.INSTANCE, new DelegatingDnsResolver(settings.getDnsResolver()), settings.getConnectionPoolTTL(), TimeUnit.MILLISECONDS); cm.setValidateAfterInactivity(settings.getValidateAfterInactivityMillis()); cm.setDefaultMaxPerRoute(settings.getMaxConnections()); cm.setMaxTotal(settings.getMaxConnections()); cm.setDefaultSocketConfig(buildSocketConfig(settings)); cm.setDefaultConnectionConfig(buildConnectionConfig(settings)); return cm; }
Example 5
Source File: ElasticsearchClient.java From elasticsearch-maven-plugin with Apache License 2.0 | 6 votes |
private static PoolingHttpClientConnectionManager buildHttpClientManager(int socketTimeout) { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(3); cm.setDefaultMaxPerRoute(2); cm.setValidateAfterInactivity(1); cm.setDefaultSocketConfig(SocketConfig.custom() .setSoTimeout(socketTimeout) .setSoLinger(0) .setTcpNoDelay(true) .build()); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { cm.close(); } }); return cm; }
Example 6
Source File: Http4FileProvider.java From commons-vfs with Apache License 2.0 | 6 votes |
private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder, final FileSystemOptions fileSystemOptions) throws FileSystemException { final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions)); connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions)); final SocketConfig socketConfig = SocketConfig .custom() .setSoTimeout(builder.getSoTimeout(fileSystemOptions)) .build(); connManager.setDefaultSocketConfig(socketConfig); return connManager; }
Example 7
Source File: HttpClientPerformanceTest.java From keycloak with Apache License 2.0 | 6 votes |
@Before public void initializeClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(Math.max(1, MAX_THREADS / 10)); connectionManager.setDefaultMaxPerRoute(connectionManager.getMaxTotal()); connectionManager.setValidateAfterInactivity(10000); connectionManager.setDefaultSocketConfig(getDefaultSocketConfig()); client = HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultCookieStore(new BasicCookieStore()) .setDefaultRequestConfig(getDefaultRequestConfig()) .setRedirectStrategy(new CustomRedirectStrategy()) .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .build(); }
Example 8
Source File: TestHttpClient4.java From davmail with GNU General Public License v2.0 | 5 votes |
public void testConnectionPooling() throws IOException { PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); poolingHttpClientConnectionManager.setDefaultMaxPerRoute(5); poolingHttpClientConnectionManager.setMaxTotal(5); poolingHttpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(1000 * 60).build()); HttpClientBuilder clientBuilder = HttpClientBuilder.create() .setConnectionManager(poolingHttpClientConnectionManager); try (CloseableHttpClient httpClient = clientBuilder.build()) { for (int i = 0; i < 10; i++) { HttpGet httpget = new HttpGet("http://davmail.sourceforge.net/version.txt"); try (CloseableHttpResponse response = httpClient.execute(httpget)) { System.out.println("Pool stats after execute: " + poolingHttpClientConnectionManager.getTotalStats()); assertEquals(1, poolingHttpClientConnectionManager.getTotalStats().getLeased()); assertEquals(0, poolingHttpClientConnectionManager.getTotalStats().getAvailable()); String responseString = new BasicResponseHandler().handleResponse(response); System.out.println(responseString); System.out.println("Pool stats after response: " + poolingHttpClientConnectionManager.getTotalStats()); } System.out.println("Pool stats after close response: " + poolingHttpClientConnectionManager.getTotalStats()); assertEquals(0, poolingHttpClientConnectionManager.getTotalStats().getLeased()); assertEquals(1, poolingHttpClientConnectionManager.getTotalStats().getAvailable()); } } System.out.println("Pool stats after close httpClient: " + poolingHttpClientConnectionManager.getTotalStats()); assertEquals(0, poolingHttpClientConnectionManager.getTotalStats().getLeased()); assertEquals(0, poolingHttpClientConnectionManager.getTotalStats().getAvailable()); }
Example 9
Source File: HttpPoolClient.java From seezoon-framework-all with Apache License 2.0 | 5 votes |
public HttpClientConnectionManager createHttpClientConnectionManager() { SSLContext sslContext = null; try { sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return false; } }).build(); } catch (Exception e) { throw new RuntimeException(e); } SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory) .build(); PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry); // 最大连接数 poolingHttpClientConnectionManager.setMaxTotal(httpClientConfig.getMaxTotal()); // 单个站点最大连接数 poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpClientConfig.getMaxPerRoute()); // 长连接 poolingHttpClientConnectionManager.setDefaultSocketConfig( SocketConfig.custom().setSoTimeout(httpClientConfig.getSocketTimeout()).setSoKeepAlive(true).build()); // 连接不活跃多久检查毫秒 并不是100 % 可信 poolingHttpClientConnectionManager.setValidateAfterInactivity(httpClientConfig.getValidateAfterInactivity()); // 空闲扫描线程 HttpClientIdleConnectionMonitor.registerConnectionManager(poolingHttpClientConnectionManager, httpClientConfig); return poolingHttpClientConnectionManager; }
Example 10
Source File: SnowizardClient.java From snowizard with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Get a new CloseableHttpClient * * @return CloseableHttpClient */ public static CloseableHttpClient newHttpClient() { final SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(Boolean.TRUE).setTcpNoDelay(Boolean.TRUE) .setSoTimeout(SOCKET_TIMEOUT_MS).build(); final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(MAX_HOSTS); manager.setMaxTotal(MAX_HOSTS); manager.setDefaultSocketConfig(socketConfig); final RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setCookieSpec(CookieSpecs.IGNORE_COOKIES) .setStaleConnectionCheckEnabled(Boolean.FALSE) .setSocketTimeout(SOCKET_TIMEOUT_MS).build(); final CloseableHttpClient client = HttpClients .custom() .disableRedirectHandling() .setConnectionManager(manager) .setDefaultRequestConfig(requestConfig) .setConnectionReuseStrategy( new DefaultConnectionReuseStrategy()) .setConnectionBackoffStrategy(new DefaultBackoffStrategy()) .setRetryHandler( new DefaultHttpRequestRetryHandler(MAX_RETRIES, false)) .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) .build(); return client; }
Example 11
Source File: HttpClientUtil.java From feiqu-opensource with Apache License 2.0 | 4 votes |
private static void init() { try { SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()), new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslSFactory = new SSLConnectionSocketFactory(sslContext); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslSFactory) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(CommonConstant.TIMEOUT).setTcpNoDelay(true).build(); connManager.setDefaultSocketConfig(socketConfig); ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build(); connManager.setDefaultConnectionConfig(connectionConfig); connManager.setMaxTotal(500); connManager.setDefaultMaxPerRoute(300); HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount > 2) { return false; } if (exception instanceof InterruptedIOException) { return true; } if (exception instanceof ConnectTimeoutException) { return true; } if (exception instanceof UnknownHostException) { return true; } if (exception instanceof SSLException) { return true; } HttpRequest request = HttpClientContext.adapt(context).getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connManager) .setRetryHandler(retryHandler) .setDefaultCookieStore(new BasicCookieStore()).setUserAgent(userAgent); if (proxy != null) { httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(proxy)).build(); } httpClient = httpClientBuilder.build(); requestConfig = RequestConfig.custom().setSocketTimeout(CommonConstant.TIMEOUT). setConnectTimeout(CommonConstant.TIMEOUT). setConnectionRequestTimeout(CommonConstant.TIMEOUT). setCookieSpec(CookieSpecs.STANDARD). build(); } catch (Exception e) { logger.error("Exception:", e); } }
Example 12
Source File: HttpSyncClient.java From Almost-Famous with MIT License | 4 votes |
public CloseableHttpClient createSyncClient(boolean proxy) throws Exception { HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory(); HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); SSLContext sslcontext = SSLContexts.createSystemDefault(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); // Create a connection manager with custom configuration. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory); // Create socket configuration SocketConfig socketConfig = SocketConfig.custom() .setTcpNoDelay(true) .build(); // Configure the connection manager to use socket configuration either // by default or for a specific host. connManager.setDefaultSocketConfig(socketConfig); connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); // Validate connections after 1 sec of inactivity connManager.setValidateAfterInactivity(1000); // Create message constraints MessageConstraints messageConstraints = MessageConstraints.custom() .setMaxHeaderCount(200) .setMaxLineLength(2000) .build(); // Create connection configuration ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints) .build(); // Configure the connection manager to use connection configuration either // by default or for a specific host. connManager.setDefaultConnectionConfig(connectionConfig); // Configure total max or per route limits for persistent connections // that can be kept in the pool or leased by the connection manager. connManager.setMaxTotal(poolSize); if (maxPerRoute > 0) { connManager.setDefaultMaxPerRoute(maxPerRoute); } else { connManager.setDefaultMaxPerRoute(10); } // Use custom cookie store if necessary. CookieStore cookieStore = new BasicCookieStore(); // Use custom credentials provider if necessary. CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // Create global request configuration RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(connectionRequestTimeout) .setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .build(); // Create an HttpClient with the given custom dependencies and configuration. CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(connManager) .setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(defaultRequestConfig) .build(); return httpclient; }
Example 13
Source File: DefaultApacheHttpClientBuilder.java From weixin-java-tools with Apache License 2.0 | 4 votes |
private synchronized void prepare() { if (prepared.get()) { return; } Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", this.plainConnectionSocketFactory) .register("https", this.sslConnectionSocketFactory) .build(); @SuppressWarnings("resource") PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(this.maxTotalConn); connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); connectionManager.setDefaultSocketConfig( SocketConfig.copy(SocketConfig.DEFAULT) .setSoTimeout(this.soTimeout) .build() ); this.idleConnectionMonitorThread = new IdleConnectionMonitorThread( connectionManager, this.idleConnTimeout, this.checkWaitTime); this.idleConnectionMonitorThread.setDaemon(true); this.idleConnectionMonitorThread.start(); HttpClientBuilder httpClientBuilder = HttpClients.custom() .setConnectionManager(connectionManager) .setConnectionManagerShared(true) .setSSLSocketFactory(this.buildSSLConnectionSocketFactory()) .setDefaultRequestConfig( RequestConfig.custom() .setSocketTimeout(this.soTimeout) .setConnectTimeout(this.connectionTimeout) .setConnectionRequestTimeout(this.connectionRequestTimeout) .build() ) .setRetryHandler(this.httpRequestRetryHandler); if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) { // 使用代理服务器 需要用户认证的代理服务器 CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials( new AuthScope(this.httpProxyHost, this.httpProxyPort), new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(provider); } if (StringUtils.isNotBlank(this.userAgent)) { httpClientBuilder.setUserAgent(this.userAgent); } this.closeableHttpClient = httpClientBuilder.build(); prepared.set(true); }
Example 14
Source File: RestTemplateConfig.java From spring-boot-cookbook with Apache License 2.0 | 4 votes |
private CloseableHttpClient getHttpClient() { //注册访问协议相关的Socket工厂 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); //HttpConnectionFactory:配置写请求/解析响应处理器 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory( DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE ); //DNS解析器 DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE; //创建连接池管理器 PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver); //设置默认的socket参数 manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待 //路由是对MaxTotal的细分。 // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。 // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool manager.setDefaultMaxPerRoute(200);//每个路由的最大连接 manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s //配置默认的请求参数 RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2 * 1000)//连接超时设置为2s .setSocketTimeout(5 * 1000)//等待数据超时设置为5s .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s // .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理 .build(); CloseableHttpClient closeableHttpClient = HttpClients.custom() .setConnectionManager(manager) .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享 .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接 .evictExpiredConnections()//回收过期连接 .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定 .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数 .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间 .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉 .build(); /** *JVM停止或重启时,关闭连接池释放掉连接 */ Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { closeableHttpClient.close(); log.info("http client closed"); } catch (IOException e) { log.error(e.getMessage(), e); } } }); return closeableHttpClient; }
Example 15
Source File: RestTemplateConfig.java From spring-boot-cookbook with Apache License 2.0 | 4 votes |
private CloseableHttpClient getHttpClient() { //注册访问协议相关的Socket工厂 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); //HttpConnectionFactory:配置写请求/解析响应处理器 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory( DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE ); //DNS解析器 DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE; //创建连接池管理器 PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver); //设置默认的socket参数 manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build()); manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待 //路由是对MaxTotal的细分。 // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。 // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool manager.setDefaultMaxPerRoute(200);//每个路由的最大连接 manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s //配置默认的请求参数 RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2 * 1000)//连接超时设置为2s .setSocketTimeout(5 * 1000)//等待数据超时设置为5s .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s // .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理 .build(); CloseableHttpClient closeableHttpClient = HttpClients.custom() .setConnectionManager(manager) .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享 .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接 .evictExpiredConnections()//回收过期连接 .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定 .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数 .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间 .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉 .build(); /** *JVM停止或重启时,关闭连接池释放掉连接 */ Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { LOGGER.info("closing http client"); closeableHttpClient.close(); LOGGER.info("http client closed"); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } }); return closeableHttpClient; }
Example 16
Source File: FDSHttpClient.java From galaxy-fds-sdk-java with Apache License 2.0 | 4 votes |
private HttpClient createHttpClient(FDSClientConfiguration config) { RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() .setConnectTimeout(config.getConnectionTimeoutMs()) .setSocketTimeout(config.getSocketTimeoutMs()); String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); requestConfigBuilder.setProxy(proxy); String proxyUsername = config.getProxyUsername(); String proxyPassword = config.getProxyPassword(); String proxyDomain = config.getProxyDomain(); String proxyWorkstation = config.getProxyWorkstation(); if (proxyUsername != null && proxyPassword != null) { credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); authCache = new BasicAuthCache(); authCache.put(proxy, new BasicScheme()); } } RequestConfig requestConfig = requestConfigBuilder.build(); SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(config.getSocketTimeoutMs()) .build(); RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create(); registryBuilder.register("http", new ConnectionInfoRecorderSocketFactory( new PlainConnectionSocketFactory())); if (config.isHttpsEnabled()) { SSLContext sslContext = SSLContexts.createSystemDefault(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionInfoRecorderSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE); registryBuilder.register("https", sslConnectionSocketFactory); } ipBlackList = new TimeBasedIpAddressBlackList(config.getIpAddressNegativeDurationMillsec()); connectionManager = new PoolingHttpClientConnectionManager(registryBuilder.build(), null, null, new RoundRobinDNSResolver(new InternalSiteBlackListDNSResolver(ipBlackList, this.dnsResolver == null ? SystemDefaultDnsResolver.INSTANCE : this.dnsResolver)), config.getHTTPKeepAliveTimeoutMS(), TimeUnit.MILLISECONDS); connectionManager.setDefaultMaxPerRoute(config.getMaxConnection()); connectionManager.setMaxTotal(config.getMaxConnection()); connectionManager.setDefaultSocketConfig(socketConfig); FDSBlackListEnabledHostChecker fdsBlackListEnabledHostChecker = new FDSBlackListEnabledHostChecker(); retryHandler = new InternalIpBlackListRetryHandler(config.getRetryCount(), ipBlackList, fdsBlackListEnabledHostChecker); return HttpClients.custom() .setRetryHandler(retryHandler) .setServiceUnavailableRetryStrategy(new ServiceUnavailableDNSBlackListStrategy( config.getRetryCount(), config.getRetryIntervalMilliSec(), ipBlackList, fdsBlackListEnabledHostChecker)) .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .build(); }