Search in sources :

Example 51 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project ovirt-engine by oVirt.

the class HttpClientBuilder method build.

public CloseableHttpClient build() throws IOException, GeneralSecurityException {
    // Prepare the default configuration for all requests:
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout != null ? connectTimeout : 0).setSocketTimeout(readTimeout != null ? readTimeout : 0).build();
    // Configure the trust manager:
    TrustManager[] trustManager = null;
    if (verifyChain) {
        if (trustStore != null) {
            try (InputStream is = new FileInputStream(trustStore)) {
                KeyStore ks = KeyStore.getInstance(trustStoreType);
                ks.load(is, StringUtils.isEmpty(trustStorePassword) ? null : trustStorePassword.toCharArray());
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm);
                tmf.init(ks);
                trustManager = tmf.getTrustManagers();
            }
        }
    } else {
        trustManager = new TrustManager[] { new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
    }
    // Create the SSL context:
    SSLContext sslContext = SSLContext.getInstance(tlsProtocol);
    sslContext.init(null, trustManager, null);
    // Create the SSL host name verifier:
    HostnameVerifier sslHostnameVerifier = null;
    if (!verifyHost) {
        sslHostnameVerifier = (hostname, session) -> true;
    }
    // Create the socket factory for HTTP:
    ConnectionSocketFactory httpSocketFactory = new PlainConnectionSocketFactory();
    // Create the socket factory for HTTPS:
    ConnectionSocketFactory httpsSocketFactory = new SSLConnectionSocketFactory(sslContext, sslHostnameVerifier);
    // Create the socket factory registry:
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpSocketFactory).register("https", httpsSocketFactory).build();
    // Create the connection manager:
    HttpClientConnectionManager connectionManager;
    if (poolSize != null) {
        PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        poolManager.setDefaultMaxPerRoute(poolSize);
        poolManager.setMaxTotal(poolSize);
        poolManager.setValidateAfterInactivity(validateAfterInactivity == null ? 100 : validateAfterInactivity);
        connectionManager = poolManager;
    } else {
        connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
    }
    // Create the client:
    return org.apache.http.impl.client.HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setSSLHostnameVerifier(sslHostnameVerifier).setConnectionManager(connectionManager).setRetryHandler(new StandardHttpRequestRetryHandler(retryCount == null ? 1 : retryCount, true)).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 52 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project gateway-dubbox by zhuzhong.

the class OpenApiHttpClientServiceImpl method initHttpClient.

private void initHttpClient() {
    // 注册访问协议相关的socket工厂
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
    // httpclient 工厂
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE);
    // dns解析器
    DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
    // 创建池化连接管理器
    manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);
    // 默认socket配置
    SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
    manager.setDefaultSocketConfig(defaultSocketConfig);
    // 设置整个连接池的最大连接数
    manager.setMaxTotal(this.maxTotal);
    // 每个路由最大连接数
    manager.setDefaultMaxPerRoute(this.defaultMaxPerRoute);
    manager.setValidateAfterInactivity(this.validateAfterInactivity);
    RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(this.connectionTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build();
    httpClient = HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(false).evictIdleConnections(60, TimeUnit.SECONDS).evictExpiredConnections().setConnectionTimeToLive(60, TimeUnit.SECONDS).setDefaultRequestConfig(defaultRequestConfig).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : DnsResolver(org.apache.http.conn.DnsResolver) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpRoute(org.apache.http.conn.routing.HttpRoute) ManagedHttpClientConnection(org.apache.http.conn.ManagedHttpClientConnection) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) ManagedHttpClientConnectionFactory(org.apache.http.impl.conn.ManagedHttpClientConnectionFactory)

Example 53 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project validator by validator.

the class PrudentHttpEntityResolver method setParams.

/**
 * Sets the timeouts of the HTTP client.
 *
 * @param connectionTimeout
 *            timeout until connection established in milliseconds. Zero
 *            means no timeout.
 * @param socketTimeout
 *            timeout for waiting for data in milliseconds. Zero means no
 *            timeout.
 * @param maxRequests
 *            maximum number of connections to a particular host
 */
public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
    PrudentHttpEntityResolver.maxRequests = maxRequests;
    PoolingHttpClientConnectionManager phcConnMgr;
    // 
    Registry<ConnectionSocketFactory> registry = // 
    RegistryBuilder.<ConnectionSocketFactory>create().register("http", // 
    PlainConnectionSocketFactory.getSocketFactory()).register("https", // 
    SSLConnectionSocketFactory.getSocketFactory()).build();
    HttpClientBuilder builder = HttpClients.custom().useSystemProperties();
    builder.setRedirectStrategy(new LaxRedirectStrategy());
    builder.setMaxConnPerRoute(maxRequests);
    builder.setMaxConnTotal(Integer.parseInt(System.getProperty("nu.validator.servlet.max-total-connections", "200")));
    if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "true"))) {
        // 
        try {
            SSLContext promiscuousSSLContext = // 
            new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
            builder.setSslcontext(promiscuousSSLContext);
            // 
            HostnameVerifier verifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            // 
            SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
            registry = // 
            RegistryBuilder.<ConnectionSocketFactory>create().register("https", // 
            promiscuousSSLConnSocketFactory).register("http", // 
            PlainConnectionSocketFactory.getSocketFactory()).build();
        } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | NumberFormatException e) {
            e.printStackTrace();
        }
    }
    phcConnMgr = new PoolingHttpClientConnectionManager(registry);
    phcConnMgr.setDefaultMaxPerRoute(maxRequests);
    phcConnMgr.setMaxTotal(200);
    builder.setConnectionManager(phcConnMgr);
    RequestConfig.Builder config = RequestConfig.custom();
    config.setCircularRedirectsAllowed(true);
    config.setMaxRedirects(Integer.parseInt(System.getProperty("nu.validator.servlet.max-redirects", "20")));
    config.setConnectTimeout(connectionTimeout);
    config.setCookieSpec(CookieSpecs.BEST_MATCH);
    config.setSocketTimeout(socketTimeout);
    config.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
    client = builder.setDefaultRequestConfig(config.build()).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder)

Example 54 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project opacclient by opacapp.

the class HttpClientFactory method getNewApacheHttpClient.

/**
 * Create a new HttpClient.
 *
 * @param tls_only If this is true, only TLS v1 and newer will be used, SSLv3 will be disabled.
 *                 We highly recommend to set this to true, if possible. This is currently a
 *                 no-op on the default implementation and only used in the Android
 *                 implementation!
 */
public HttpClient getNewApacheHttpClient(boolean customssl, boolean tls_only, boolean allCipherSuites, boolean disguise_app) {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setRedirectStrategy(new CustomRedirectStrategy());
    if (disguise_app) {
        builder.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, " + "like Gecko) Chrome/43.0.2357.130 Safari/537.36\t");
    } else {
        builder.setUserAgent(user_agent);
    }
    if (customssl && ssl_store_path != null) {
        try {
            if (trust_store == null) {
                trust_store = getKeyStore();
            }
            SSLConnectionSocketFactory sf = AdditionalKeyStoresSSLSocketFactory.create(getSocketFactoryClass(tls_only, allCipherSuites), new AdditionalKeyStoresSSLSocketFactory.AdditionalKeyStoresTrustManager(trust_store));
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sf).build();
            HttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
            builder.setConnectionManager(ccm);
            return builder.build();
        } catch (Exception e) {
            e.printStackTrace();
            return builder.build();
        }
    } else {
        return builder.build();
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ProtocolException(org.apache.http.ProtocolException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CircularRedirectException(org.apache.http.client.CircularRedirectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 55 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project camunda-bpm-platform by camunda.

the class RestJaxRs2IT method shouldPerform500ConcurrentRequests.

@Test
public void shouldPerform500ConcurrentRequests() throws InterruptedException, ExecutionException {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    Callable<String> performRequest = new Callable<String>() {

        @Override
        public String call() throws IOException {
            HttpPost request = new HttpPost(APP_BASE_PATH + FETCH_AND_LOCK_PATH);
            request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            StringEntity stringEntity = new StringEntity("{ \"workerId\": \"aWorkerId\", \"asyncResponseTimeout\": 1000 }");
            request.setEntity(stringEntity);
            CloseableHttpResponse response = httpClient.execute(request, HttpClientContext.create());
            String responseBody = null;
            try {
                HttpEntity entity = response.getEntity();
                responseBody = EntityUtils.toString(entity);
                request.releaseConnection();
            } finally {
                response.close();
            }
            return responseBody;
        }
    };
    int requestsCount = 500;
    ExecutorService service = Executors.newFixedThreadPool(requestsCount);
    List<Callable<String>> requests = new ArrayList<Callable<String>>();
    for (int i = 0; i < requestsCount; i++) {
        requests.add(performRequest);
    }
    List<Future<String>> futures = service.invokeAll(requests);
    service.shutdown();
    service.awaitTermination(1, TimeUnit.HOURS);
    for (Future<String> future : futures) {
        assertEquals(future.get(), "[]");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test) AbstractWebappIntegrationTest(org.camunda.bpm.AbstractWebappIntegrationTest)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12