org.apache.http.impl.conn.PoolingHttpClientConnectionManager Java Examples

The following examples show how to use org.apache.http.impl.conn.PoolingHttpClientConnectionManager. 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 Project: vk-java-sdk   Author: VKCOM   File: HttpTransportClient.java    License: MIT License 7 votes vote down vote up
public HttpTransportClient(int retryAttemptsNetworkErrorCount, int retryAttemptsInvalidStatusCount) {
    this.retryAttemptsNetworkErrorCount = retryAttemptsNetworkErrorCount;
    this.retryAttemptsInvalidStatusCount = retryAttemptsInvalidStatusCount;

    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(SOCKET_TIMEOUT_MS)
            .setConnectTimeout(CONNECTION_TIMEOUT_MS)
            .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS);

    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig)
            .setDefaultCookieStore(cookieStore)
            .setUserAgent(USER_AGENT)
            .build();
}
 
Example #2
Source Project: ScriptSpider   Author: xjtushilei   File: HttpUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建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 #3
Source Project: ATest   Author: shirleydl   File: HttpClientUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static CloseableHttpClient getHttpClient(final int executionCount, int retryInterval) {
	ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new MyServiceUnavailableRetryStrategy.Builder()
			.executionCount(executionCount).retryInterval(retryInterval).build();
	return HttpClientBuilder.create().setRetryHandler(new HttpRequestRetryHandler() {
		@Override
		public boolean retryRequest(IOException e, int count, HttpContext contr) {
			if (count >= executionCount) {
				// Do not retry if over max retry count
				return false;
			}
			if (e instanceof InterruptedIOException) {
				// Timeout
				return true;
			}

			return true;
		}
	}).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
			.setConnectionManager(new PoolingHttpClientConnectionManager()).build();
}
 
Example #4
Source Project: ATest   Author: shirleydl   File: HttpClientUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static CloseableHttpClient getHttpClient(final int executionCount, int retryInterval) {
	ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new MyServiceUnavailableRetryStrategy.Builder()
			.executionCount(executionCount).retryInterval(retryInterval).build();
	return HttpClientBuilder.create().setRetryHandler(new HttpRequestRetryHandler() {
		@Override
		public boolean retryRequest(IOException e, int count, HttpContext contr) {
			if (count >= executionCount) {
				// Do not retry if over max retry count
				return false;
			}
			if (e instanceof InterruptedIOException) {
				// Timeout
				return true;
			}

			return true;
		}
	}).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
			.setConnectionManager(new PoolingHttpClientConnectionManager()).build();
}
 
Example #5
Source Project: smart-admin   Author: 1024-lab   File: SmartRestTemplateConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public HttpClient httpClient() {
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSocketFactory())
            .build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(maxTotal);
    connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .build();
    return HttpClientBuilder.create()
            .setDefaultRequestConfig(requestConfig)
            .setConnectionManager(connectionManager)
            .build();
}
 
Example #6
Source Project: smockin   Author: mgtechsoftware   File: HttpClientServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient noSslHttpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

        final SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (x509CertChain, authType) -> true)
                .build();

        return HttpClientBuilder.create()
                .setSSLContext(sslContext)
                .setConnectionManager(
                        new PoolingHttpClientConnectionManager(
                                RegistryBuilder.<ConnectionSocketFactory>create()
                                        .register("http", PlainConnectionSocketFactory.INSTANCE)
                                        .register("https", new SSLConnectionSocketFactory(sslContext,
                                                NoopHostnameVerifier.INSTANCE))
                                        .build()
                        ))
                .build();
    }
 
Example #7
Source Project: ZTuoExchange_framework   Author: sengeiou   File: HttpClient4Utils.java    License: MIT License 6 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 #8
Source Project: github-autostatus-plugin   Author: jenkinsci   File: AbstractNotifierConfig.java    License: MIT License 6 votes vote down vote up
/**
 * Gets an HTTP client that can be used to make requests.
 *
 * @return HTTP client
 */
public CloseableHttpClient getHttpClient(boolean ignoreSSL) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    if (ignoreSSL) {
        final SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (x509CertChain, authType) -> true)
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.INSTANCE)
                        .register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
                        .build()
        );
        return HttpClientBuilder.create()
                .setSSLContext(sslContext)
                .setConnectionManager(connectionManager)
                .build();
    }
    return HttpClients.createDefault();
}
 
Example #9
Source Project: Asqatasun   Author: Asqatasun   File: HttpRequestHandler.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private CloseableHttpClient getHttpClient(String url) {
     RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectionTimeout)
.build();
     HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
     httpClientBuilder.setDefaultRequestConfig(requestConfig);
     httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager());
     httpClientBuilder.setUserAgent(ASQATASUN_USER_AGENT);
     if (isProxySet(url)) {
         LOGGER.debug(("Set proxy with " + proxyHost + " and " + proxyPort));
         httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.valueOf(proxyPort)));
         if (isProxyCredentialSet()) {
             CredentialsProvider credsProvider = new BasicCredentialsProvider();
             credsProvider.setCredentials(
                     new AuthScope(proxyHost, Integer.valueOf(proxyPort)),
                     new UsernamePasswordCredentials(proxyUser, proxyPassword));
             httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
             LOGGER.debug(("Set proxy credentials " + proxyHost + " and " + proxyPort + " and " + proxyUser + " and " + proxyPassword));
         }
     }
     return httpClientBuilder.build();
 }
 
Example #10
Source Project: spring-cloud-formula   Author: baidu   File: BmsHttpTransport.java    License: Apache License 2.0 6 votes vote down vote up
public BmsHttpTransport(HttpRequestInterceptor requestInterceptor) {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(DEFAULT_MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE_CONNECTIONS);

    RequestConfig requestConfig = RequestConfig.custom().
            setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT).
            setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT).
            setSocketTimeout(DEFAULT_READ_TIMEOUT).
            build();

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().
            setConnectionManager(connectionManager).
            setDefaultRequestConfig(requestConfig).
            useSystemProperties();

    if (requestInterceptor != null) {
        httpClientBuilder.addInterceptorFirst(requestInterceptor);
    }

    this.httpClient = httpClientBuilder.build();
}
 
Example #11
Source Project: arcusplatform   Author: arcus-smart-home   File: HomeGraphAPI.java    License: 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 #12
Source Project: dtsopensource   Author: adealjason   File: HttpProtocolParent.java    License: Apache License 2.0 6 votes vote down vote up
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 #13
Source Project: carbon-device-mgt   Author: wso2   File: RESTInvoker.java    License: Apache License 2.0 6 votes vote down vote up
private void configureHttpClient() {
    int connectionTimeout = 120000;
    int socketTimeout = 120000;
    int maxTotalConnectionsPerRoute = 100;
    int maxTotalConnections = 100;
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setExpectContinueEnabled(true)
            .setConnectTimeout(connectionTimeout)
            .setSocketTimeout(socketTimeout)
            .build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(maxTotalConnectionsPerRoute);
    connectionManager.setMaxTotal(maxTotalConnections);
    client = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(defaultRequestConfig)
            .build();
    if (log.isDebugEnabled()) {
        log.debug("REST client initialized with " +
                "maxTotalConnection = " + maxTotalConnections +
                "maxConnectionsPerRoute = " + maxTotalConnectionsPerRoute +
                "connectionTimeout = " + connectionTimeout);
    }

}
 
Example #14
Source Project: SpringBootBucket   Author: yidao620c   File: HttpClientConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
    return new Runnable() {
        @Override
        @Scheduled(fixedDelay = 10000)
        public void run() {
            try {
                if (connectionManager != null) {
                    LOGGER.trace("run IdleConnectionMonitor - Closing expired and idle connections...");
                    connectionManager.closeExpiredConnections();
                    connectionManager.closeIdleConnections(p.getCloseIdleConnectionWaitTimeSecs(), TimeUnit.SECONDS);
                } else {
                    LOGGER.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
                }
            } catch (Exception e) {
                LOGGER.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
            }
        }
    };
}
 
Example #15
Source Project: rapid   Author: ozlerhakan   File: DockerClient.java    License: MIT License 6 votes vote down vote up
private void init() {
    final URI originalUri = URI.create(DEFAULT_UNIX_ENDPOINT);
    sanitizeUri = UnixFactory.sanitizeUri(originalUri);

    final RegistryBuilder<ConnectionSocketFactory> registryBuilder =
            RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("https", SSLConnectionSocketFactory.getSocketFactory())
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("unix", new UnixFactory(originalUri));

    final PoolingHttpClientConnectionManager cm =
            new PoolingHttpClientConnectionManager(registryBuilder.build());

    final RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout((int) SECONDS.toMillis(5))
            .setConnectTimeout((int) SECONDS.toMillis(5))
            .setSocketTimeout((int) SECONDS.toMillis(30))
            .build();

    final ClientConfig config = new ClientConfig()
            .connectorProvider(new ApacheConnectorProvider())
            .property(ApacheClientProperties.CONNECTION_MANAGER, cm)
            .property(ApacheClientProperties.REQUEST_CONFIG, requestConfig);

    client = ClientBuilder.newBuilder().withConfig(config).build();
}
 
Example #16
Source Project: davmail   Author: mguessan   File: TestHttpClient4.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testSSL() throws IOException {

        RegistryBuilder<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.create();

        schemeRegistry.register("https", new SSLConnectionSocketFactory(new DavGatewaySSLSocketFactory(),
                SSLConnectionSocketFactory.getDefaultHostnameVerifier()));

        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(schemeRegistry.build());
        HttpClientBuilder clientBuilder = HttpClientBuilder.create()
                .disableRedirectHandling()
                .setConnectionManager(poolingHttpClientConnectionManager);
        try (CloseableHttpClient httpClient = clientBuilder.build()) {

            HttpGet httpget = new HttpGet("https://outlook.office365.com");
            try (CloseableHttpResponse response = httpClient.execute(httpget)) {
                assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatusLine().getStatusCode());
            }
        }
    }
 
Example #17
Source Project: riptide   Author: zalando   File: HttpClientFactory.java    License: MIT License 6 votes vote down vote up
public static HttpClientConnectionManager createHttpClientConnectionManager(final Client client)
        throws GeneralSecurityException, IOException {

    final Connections connections = client.getConnections();

    final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
            RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", new SSLConnectionSocketFactory(createSSLContext(client)))
                    .build(),
            null, // connection factory
            null, // scheme port resolver
            null, // dns resolver
            connections.getTimeToLive().getAmount(),
            connections.getTimeToLive().getUnit());

    manager.setMaxTotal(connections.getMaxTotal());
    manager.setDefaultMaxPerRoute(connections.getMaxPerRoute());

    return manager;
}
 
Example #18
Source Project: tutorials   Author: eugenp   File: HttpClientConnectionManagementLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
// @Ignore
// 4.2 Tester Version
/*tester*/ public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException, IOException {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
    final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
    final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
    final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
    thread1.start();
    thread2.start();
    thread3.start();
    thread1.join(10000);
    thread2.join(10000);
    thread3.join(10000);
}
 
Example #19
Source Project: yacy_grid_mcp   Author: yacy   File: ClientConnection.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
public static PoolingHttpClientConnectionManager getConnctionManager(){

        Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
        try {
            SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
                        new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
                        new TrustAllHostNameVerifier());
            socketFactoryRegistry = RegistryBuilder
                    .<ConnectionSocketFactory> create()
                    .register("http", new PlainConnectionSocketFactory())
                    .register("https", trustSelfSignedSocketFactory)
                    .build();
        } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
            Data.logger.warn("", e);
        }
        
        PoolingHttpClientConnectionManager cm = (socketFactoryRegistry != null) ? 
                new PoolingHttpClientConnectionManager(socketFactoryRegistry):
                new PoolingHttpClientConnectionManager();
        
        // twitter specific options
        cm.setMaxTotal(2000);
        cm.setDefaultMaxPerRoute(200);
        
        return cm;
    }
 
Example #20
Source Project: data-highway   Author: HotelsDotCom   File: HttpClientConnectionManagerFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWithThreadsConfiguration() throws Exception {
  HttpClientConnectionManager connectionManager = HttpClientConnectionManagerFactory.create(3, null);

  assertThat(connectionManager, is(instanceOf(PoolingHttpClientConnectionManager.class)));

  PoolingHttpClientConnectionManager poolingConnectionManager = (PoolingHttpClientConnectionManager) connectionManager;
  assertThat(poolingConnectionManager.getDefaultMaxPerRoute(), is(9));
  assertThat(poolingConnectionManager.getMaxTotal(), is(9));
  assertThat(poolingConnectionManager.getValidateAfterInactivity(), is(1000));
}
 
Example #21
Source Project: netcdf-java   Author: Unidata   File: HTTPConnections.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected PoolingHttpClientConnectionManager getPool() {
  if (poolmgr == null) {
    this.poolmgr = new PoolingHttpClientConnectionManager(getRegistry());
    setMaxConnections(this.maxconnections);
  }
  return this.poolmgr;
}
 
Example #22
Source Project: Qualitis   Author: WeBankFinTech   File: RestClientConfig.java    License: Apache License 2.0 5 votes vote down vote up
private HttpClient httpClient() {
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSocketFactory())
            .build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    // Set max total connection
    connectionManager.setMaxTotal(maxTotal);
    // Set max route connection
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .build();
    return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
            .setConnectionManager(connectionManager)
            .build();
}
 
Example #23
Source Project: MixPush   Author: taoweiji   File: IgnoreSSLUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * for ignoring verify SSL
 */
public static CloseableHttpClient createClient() throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext sslcontext = createIgnoreVerifySSL();

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext))
            .build();
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    HttpClients.custom().setConnectionManager(connManager);

    return HttpClients.custom().setConnectionManager(connManager).build();
}
 
Example #24
Source Project: vk-java-sdk   Author: VKCOM   File: YouTrackClient.java    License: MIT License 5 votes vote down vote up
public YouTrackClient(String host, String keyStoreType, String keyStorePath, String keyStorePassword, String keyPassword,
                      String trustStoreType, String trustStorePath, String trustStorePassword) {
    this.host = host;

    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(SOCKET_TIMEOUT_MS)
            .setConnectTimeout(CONNECTION_TIMEOUT_MS)
            .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();

    LayeredConnectionSocketFactory sslFactory;

    RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", new PlainConnectionSocketFactory());

    if (host.contains("https://")) {
        try {
            sslFactory = initSslContext(keyStoreType, keyStorePath, keyStorePassword, keyPassword, trustStoreType, trustStorePath, trustStorePassword);
            registryBuilder.register("https", sslFactory);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    Registry<ConnectionSocketFactory> registry = registryBuilder.build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);

    connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS);

    client = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig)
            .setDefaultCookieStore(cookieStore)
            .build();
}
 
Example #25
Source Project: message_interface   Author: qiyu-kefu   File: HttpClientPool.java    License: MIT License 5 votes vote down vote up
private static CloseableHttpClient client() {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setDefaultMaxPerRoute(100);
    cm.setMaxTotal(400);

    MessageConstraints messageConstraints = MessageConstraints.custom()
            .setMaxHeaderCount(200)
            .setMaxLineLength(2000)
            .build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8)
            .setMessageConstraints(messageConstraints)
            .build();

    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(5000)
            .setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000)
            .build();

    cm.setDefaultConnectionConfig(connectionConfig);

    return HttpClients.custom()
            .setConnectionManager(cm)
            .setDefaultRequestConfig(defaultRequestConfig)
            .build();
}
 
Example #26
Source Project: mycore   Author: MyCoRe-Org   File: MCRHttpUtils.java    License: 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 #27
Source Project: hop   Author: project-hop   File: ServerConnectionManager.java    License: Apache License 2.0 5 votes vote down vote up
private ServerConnectionManager() {
  if ( needToInitializeSSLContext() ) {
    try {
      SSLContext context = SSLContext.getInstance( SSL );
      context.init( new KeyManager[ 0 ], new X509TrustManager[] { getDefaultTrustManager() }, new SecureRandom() );
      SSLContext.setDefault( context );
    } catch ( Exception e ) {
      //log.logError( "Default SSL context hasn't been initialized", e );
    }
  }
  manager = new PoolingHttpClientConnectionManager();
  manager.setDefaultMaxPerRoute( 100 );
  manager.setMaxTotal( 200 );
}
 
Example #28
Source Project: lucene-solr   Author: apache   File: ConnectionReuseTest.java    License: Apache License 2.0 5 votes vote down vote up
public void headerRequest(HttpHost target, HttpRoute route, HttpClientConnection conn, PoolingHttpClientConnectionManager cm)
    throws IOException, HttpException {
  HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);

  req.addHeader("Host", target.getHostName());
  if (!conn.isOpen()) {
    // establish connection based on its route info
    cm.connect(conn, route, 1000, context);
    // and mark it as route complete
    cm.routeComplete(conn, route, context);
  }
  conn.sendRequestHeader(req);
  conn.flush();
  conn.receiveResponseHeader();
}
 
Example #29
Source Project: tutorials   Author: eugenp   File: HttpClientConnectionManagementLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
// Example 3.1.
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws InterruptedException, ClientProtocolException, IOException {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
    client.execute(get1);

    assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
}
 
Example #30
Source Project: incubator-gobblin   Author: apache   File: AbstractHttpWriterBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public B fromConfig(Config config) {
  config = config.withFallback(FALLBACK);
  RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
                                             .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY))
                                             .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY))
                                             .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY))
                                             .build();

  getHttpClientBuilder().setDefaultRequestConfig(requestConfig);

  if (config.hasPath(STATIC_SVC_ENDPOINT)) {
    try {
      svcEndpoint = Optional.of(new URI(config.getString(AbstractHttpWriterBuilder.STATIC_SVC_ENDPOINT)));
    } catch (URISyntaxException e) {
      throw new RuntimeException(e);
    }
  }

  String connMgrStr = config.getString(HTTP_CONN_MANAGER);
  switch (ConnManager.valueOf(connMgrStr.toUpperCase())) {
    case BASIC:
      httpConnManager = new BasicHttpClientConnectionManager();
      break;
    case POOLING:
      PoolingHttpClientConnectionManager poolingConnMgr = new PoolingHttpClientConnectionManager();
      poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN));
      poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN));
      httpConnManager = poolingConnMgr;
      break;
    default:
      throw new IllegalArgumentException(connMgrStr + " is not supported");
  }
  LOG.info("Using " + httpConnManager.getClass().getSimpleName());
  return typedSelf();
}