Java Code Examples for org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setValidateAfterInactivity()

The following examples show how to use org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setValidateAfterInactivity() . 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: MPRestClient.java    From dx-java with MIT License 10 votes vote down vote up
/**
 * Create a HttpClient
 * @return a HttpClient
 */
private HttpClient createHttpClient() {
    SSLContext sslContext = SSLContexts.createDefault();
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            new String[]{"TLSv1.1", "TLSv1.2"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory)
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(MercadoPago.SDK.getMaxConnections());
    connectionManager.setDefaultMaxPerRoute(MercadoPago.SDK.getMaxConnections());
    connectionManager.setValidateAfterInactivity(VALIDATE_INACTIVITY_INTERVAL_MS);

    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(MercadoPago.SDK.getRetries(), false);

    HttpClientBuilder httpClientBuilder = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setKeepAliveStrategy(new KeepAliveStrategy())
            .setRetryHandler(retryHandler)
            .disableCookieManagement()
            .disableRedirectHandling();

    return httpClientBuilder.build();
}
 
Example 2
Source File: HttpClient4Utils.java    From ZTuoExchange_framework with MIT License 8 votes vote down vote up
/**
 * 实例化HttpClient
 *
 * @param maxTotal
 * @param maxPerRoute
 * @param socketTimeout
 * @param connectTimeout
 * @param connectionRequestTimeout
 * @return
 */
public static HttpClient createHttpClient(int maxTotal, int maxPerRoute, int socketTimeout, int connectTimeout,
                                          int connectionRequestTimeout) {
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(maxTotal);
    cm.setDefaultMaxPerRoute(maxPerRoute);
    cm.setValidateAfterInactivity(200); // 一个连接idle超过200ms,再次被使用之前,需要先做validation
    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setConnectionTimeToLive(30, TimeUnit.SECONDS)
            .setRetryHandler(new StandardHttpRequestRetryHandler(3, true)) // 配置出错重试
            .setDefaultRequestConfig(defaultRequestConfig).build();

    startMonitorThread(cm);

    return httpClient;
}
 
Example 3
Source File: EntitlementAPITest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private AuthzClient getAuthzClient(String configFile) {
    if (authzClient == null) {
        Configuration configuration;
        try {
            configuration = JsonSerialization.readValue(httpsAwareConfigurationStream(getClass().getResourceAsStream("/authorization-test/" + configFile)), Configuration.class);
        } catch (IOException e) {
            throw new RuntimeException("Failed to read configuration", e);
        }
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setValidateAfterInactivity(10);
        connectionManager.setMaxTotal(10);
        HttpClient client = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .build();
        authzClient = AuthzClient.create(new Configuration(configuration.getAuthServerUrl(), configuration.getRealm(), configuration.getResource(), configuration.getCredentials(), client));
    }

    return authzClient;
}
 
Example 4
Source File: ElasticsearchClient.java    From elasticsearch-maven-plugin with Apache License 2.0 6 votes vote down vote up
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 5
Source File: ConnectionManager.java    From curly with Apache License 2.0 6 votes vote down vote up
private void createNewConnectionManager() {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                builder.build(), NoopHostnameVerifier.INSTANCE);
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new PlainConnectionSocketFactory())
                .register("https", sslsf)
                .build();
        connectionManager = new PoolingHttpClientConnectionManager(r);
        connectionManager.setValidateAfterInactivity(500);
        sharedContext = ThreadLocal.withInitial(HttpClientContext::new);
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example 6
Source File: HttpClientPerformanceTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: SeedManager.java    From joal with Apache License 2.0 6 votes vote down vote up
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 8
Source File: HomeGraphAPI.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Inject
public HomeGraphAPI(GoogleConfig config,
      GoogleRpcContext rpcContext,
      ProductCatalogManager prodCat,
      GoogleWhitelist whitelist,
      @Named(EXECUTOR_NAME) HashedWheelTimer executor
) {
   this.config = config;
   requestConfig = RequestConfig.custom()
      .setConnectionRequestTimeout(config.getConnectionRequestTimeoutMs())
      .setConnectTimeout(config.getConnectionTimeoutMs())
      .setSocketTimeout(config.getSocketTimeoutMs())
      .build();

   pool = new PoolingHttpClientConnectionManager(config.getTimeToLiveMs(), TimeUnit.MILLISECONDS);
   pool.setDefaultMaxPerRoute(config.getRouteMaxConnections());
   pool.setMaxTotal(config.getMaxConnections());
   pool.setValidateAfterInactivity(config.getValidateAfterInactivityMs());
   this.gRpcContext = rpcContext;
   this.prodCat = prodCat;
   this.whitelist = whitelist;
   this.executor = executor;
}
 
Example 9
Source File: HttpConnectionPoolBuilder.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
public PoolingHttpClientConnectionManager createConnectionManager(final Registry<ConnectionSocketFactory> registry) {
    if(log.isDebugEnabled()) {
        log.debug(String.format("Setup connection pool with registry %s", registry));
    }
    final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
    manager.setMaxTotal(preferences.getInteger("http.connections.total"));
    manager.setDefaultMaxPerRoute(preferences.getInteger("http.connections.route"));
    // Detect connections that have become stale (half-closed) while kept inactive in the pool
    manager.setValidateAfterInactivity(preferences.getInteger("http.connections.stale.check.ms"));
    return manager;
}
 
Example 10
Source File: HttpUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 获取HttpClient
 *
 * @return 获取HttpClient
 */
private static CloseableHttpClient globalHttpClient() {
    PoolingHttpClientConnectionManager connectionManager = getConnectionManager();
    connectionManager.setMaxTotal(TOTAL_MAX_CONNECTION);
    connectionManager.setDefaultMaxPerRoute(MAX_CONNECTION_PER_ROUTE);
    connectionManager.setValidateAfterInactivity(1);
    return HttpClients.custom().setConnectionManager(connectionManager).build();
}
 
Example 11
Source File: HttpPoolClient.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
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 12
Source File: HttpClientConnectionManagerFactory.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private static PoolingHttpClientConnectionManager createClientConnectionManager(
    Registry<ConnectionSocketFactory> registry,
    int threads) {
  PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
  connectionManager.setDefaultMaxPerRoute(threads * MAX_CONNECTIONS_MULTIPLIER);
  connectionManager.setMaxTotal(threads * MAX_CONNECTIONS_MULTIPLIER);
  connectionManager.setValidateAfterInactivity(1000);
  return connectionManager;
}
 
Example 13
Source File: MCRHttpUtils.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static PoolingHttpClientConnectionManager getConnectionManager(int maxConnections) {
    //configure connection manager
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(maxConnections);
    connectionManager.setMaxTotal(maxConnections);
    connectionManager.setValidateAfterInactivity(30000);
    return connectionManager;
}
 
Example 14
Source File: InsightDataManager.java    From dx-java with MIT License 5 votes vote down vote up
/**
 * Create a HttpClient
 * 
 * @return a HttpClient
 */
private HttpClient createHttpClient() {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MercadoPago.SDK.getMaxConnections());
    connectionManager.setValidateAfterInactivity(VALIDATE_INACTIVITY_INTERVAL_MS);
    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(MercadoPago.SDK.getRetries(),
            false);

    HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager)
            .setKeepAliveStrategy(new KeepAliveStrategy()).setRetryHandler(retryHandler).disableCookieManagement()
            .disableRedirectHandling();

    return httpClientBuilder.build();
}
 
Example 15
Source File: OptimizelyHttpClient.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
public OptimizelyHttpClient build() {
    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setMaxTotal(maxTotalConnections);
    poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
    poolingHttpClientConnectionManager.setValidateAfterInactivity(validateAfterInactivity);

    CloseableHttpClient closableHttpClient = HttpClients.custom()
        .setDefaultRequestConfig(HttpClientUtils.DEFAULT_REQUEST_CONFIG)
        .setConnectionManager(poolingHttpClientConnectionManager)
        .disableCookieManagement()
        .useSystemProperties()
        .build();

    return new OptimizelyHttpClient(closableHttpClient);
}
 
Example 16
Source File: AlexaHttpClient.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Inject
public AlexaHttpClient(AlexaConfig config) {
   this.config = config;
   requestConfig = RequestConfig.custom()
      .setConnectionRequestTimeout(config.getConnectionRequestTimeoutMs())
      .setConnectTimeout(config.getConnectionTimeoutMs())
      .setSocketTimeout(config.getSocketTimeoutMs())
      .build();

   pool = new PoolingHttpClientConnectionManager(config.getTimeToLiveMs(), TimeUnit.MILLISECONDS);
   pool.setDefaultMaxPerRoute(config.getRouteMaxConnections());
   pool.setMaxTotal(config.getMaxConnections());
   pool.setValidateAfterInactivity(config.getValidateAfterInactivityMs());
}
 
Example 17
Source File: GoogleApacheHttpTransport.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new instance of {@link ApacheHttpTransport} that uses
 * {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates.
 */
public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException,
    IOException {
  // Set socket buffer sizes to 8192
  SocketConfig socketConfig =
      SocketConfig.custom()
          .setRcvBufSize(8192)
          .setSndBufSize(8192)
          .build();

  PoolingHttpClientConnectionManager connectionManager =
      new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);

  // Disable the stale connection check (previously configured in the HttpConnectionParams
  connectionManager.setValidateAfterInactivity(-1);

  // Use the included trust store
  KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
  SSLContext sslContext = SslUtils.getTlsSslContext();
  SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory());
  LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);

  HttpClient client = HttpClientBuilder.create()
      .useSystemProperties()
      .setSSLSocketFactory(socketFactory)
      .setDefaultSocketConfig(socketConfig)
      .setMaxConnTotal(200)
      .setMaxConnPerRoute(20)
      .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
      .setConnectionManager(connectionManager)
      .disableRedirectHandling()
      .disableAutomaticRetries()
      .build();
  return new ApacheHttpTransport(client);
}
 
Example 18
Source File: HttpClientConnectionManagementLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
// @Ignore
// 8.1
public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    poolingConnManager.setValidateAfterInactivity(1000);
    client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()).setConnectionManager(poolingConnManager).build();
}
 
Example 19
Source File: HttpSyncClient.java    From Almost-Famous with MIT License 4 votes vote down vote up
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 20
Source File: RestTemplateConfig.java    From spring-boot-cookbook with Apache License 2.0 4 votes vote down vote up
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;
    }