Search in sources :

Example 11 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project aliyun-oss-java-sdk by aliyun.

the class IdleConnectionReaper method run.

@SuppressWarnings("unchecked")
@Override
public void run() {
    while (true) {
        if (shuttingDown) {
            getLog().debug("Shutting down reaper thread.");
            return;
        }
        try {
            Thread.sleep(REAP_INTERVAL_MILLISECONDS);
        } catch (InterruptedException e) {
        }
        try {
            List<HttpClientConnectionManager> connectionManagers = null;
            synchronized (IdleConnectionReaper.class) {
                connectionManagers = (List<HttpClientConnectionManager>) IdleConnectionReaper.connectionManagers.clone();
            }
            for (HttpClientConnectionManager connectionManager : connectionManagers) {
                try {
                    connectionManager.closeExpiredConnections();
                    connectionManager.closeIdleConnections(idleConnectionTime, TimeUnit.MILLISECONDS);
                } catch (Exception ex) {
                    getLog().warn("Unable to close idle connections", ex);
                }
            }
        } catch (Throwable t) {
            getLog().debug("Reaper thread: ", t);
        }
    }
}
Also used : HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager)

Example 12 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project hbase by apache.

the class TestSecureRESTServer method getClient.

private Pair<CloseableHttpClient, HttpClientContext> getClient() {
    HttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
    HttpHost host = new HttpHost("localhost", REST_TEST.getServletPort());
    Registry<AuthSchemeProvider> authRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)).build();
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, EmptyCredentials.INSTANCE);
    AuthCache authCache = new BasicAuthCache();
    CloseableHttpClient client = HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry).setConnectionManager(pool).build();
    HttpClientContext context = HttpClientContext.create();
    context.setTargetHost(host);
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthSchemeRegistry(authRegistry);
    context.setAuthCache(authCache);
    return new Pair<>(client, context);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpHost(org.apache.http.HttpHost) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) Pair(org.apache.hadoop.hbase.util.Pair)

Example 13 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project wildfly by wildfly.

the class TestHttpClientUtils method getHttpsClient.

/**
 *@param credentialsProvider optional cred provider
 * @return client that doesn't verify https connections
 */
public static CloseableHttpClient getHttpsClient(CredentialsProvider credentialsProvider) {
    try {
        SSLContext ctx = getSslContext();
        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx, new NoopHostnameVerifier());
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionFactory).build();
        HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
        HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(sslConnectionFactory).setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm);
        if (credentialsProvider != null) {
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
        return builder.build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) SSLContext(javax.net.ssl.SSLContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 14 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project canal by alibaba.

the class AbstractRequest method executeHttpRequest.

/**
 * 执行http请求
 *
 * @param getMethod
 * @return
 * @throws IOException
 */
@SuppressWarnings("deprecation")
private final HttpResponse executeHttpRequest(HttpGet getMethod, String host) throws Exception {
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (TrustStrategy) (arg0, arg1) -> true).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Registry registry = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslsf).build();
    HttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
    CloseableHttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(50).setMaxConnTotal(100).setConnectionManager(httpClientConnectionManager).build();
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
    getMethod.setConfig(requestConfig);
    HttpResponse response = httpClient.execute(getMethod);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpResponseStatus.OK.code() && statusCode != HttpResponseStatus.PARTIAL_CONTENT.code()) {
        String result = EntityUtils.toString(response.getEntity());
        throw new RuntimeException("return error !" + response.getStatusLine().getReasonPhrase() + ", " + result);
    }
    return response;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) Registry(org.apache.http.config.Registry) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 15 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project docker-maven-plugin by fabric8io.

the class HttpClientBuilder method buildPooledClient.

public CloseableHttpClient buildPooledClient() throws IOException {
    org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom();
    HttpClientConnectionManager manager = getPooledConnectionFactory(certPath, maxConnections);
    builder.setConnectionManager(manager);
    return builder.build();
}
Also used : org.apache.http.impl.client(org.apache.http.impl.client) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

HttpClientConnectionManager (org.apache.http.conn.HttpClientConnectionManager)35 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)23 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)19 IOException (java.io.IOException)16 HttpResponse (org.apache.http.HttpResponse)14 HttpClient (org.apache.http.client.HttpClient)11 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)11 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 SSLContext (javax.net.ssl.SSLContext)8 RequestConfig (org.apache.http.client.config.RequestConfig)8 CertificateException (java.security.cert.CertificateException)7 HttpGet (org.apache.http.client.methods.HttpGet)6 HttpPost (org.apache.http.client.methods.HttpPost)6 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 KeyManagementException (java.security.KeyManagementException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)4 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)4