Search in sources :

Example 26 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project csb-sdk by aliyun.

the class HttpCaller method createAsyncHttpClient.

private static CloseableHttpAsyncClient createAsyncHttpClient(String requestURL) throws HttpCallerException {
    CloseableHttpAsyncClient httpClient = null;
    if (isSSLProtocol(requestURL)) {
        try {
            httpClient = HttpAsyncClients.custom().setSSLHostnameVerifier(new org.apache.http.conn.ssl.NoopHostnameVerifier()).setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build()).build();
        } catch (KeyManagementException e) {
            throw new HttpCallerException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new HttpCallerException(e);
        } catch (KeyStoreException e) {
            throw new HttpCallerException(e);
        }
    } else {
        httpClient = HttpAsyncClients.createDefault();
    }
    httpClient.start();
    return httpClient;
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder)

Example 27 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project csb-sdk by aliyun.

the class HttpCaller method doAsyncHttpReq.

private static String doAsyncHttpReq(String requestURL, HttpRequestBase httpRequestBase, final StringBuffer resHttpHeaders) throws HttpCallerException {
    if (DEBUG) {
        HttpClientHelper.printDebugInfo("doAsyncHttpReq ");
    }
    long startT = System.currentTimeMillis();
    HttpResponse response = null;
    CloseableHttpAsyncClient httpClient = createAsyncHttpClient(requestURL);
    if (DEBUG) {
        HttpClientHelper.printDebugInfo("--+++ get httpclient costs = " + (System.currentTimeMillis() - startT) + " ms ");
        startT = System.currentTimeMillis();
    }
    try {
        try {
            httpClient.start();
            Future<HttpResponse> asyncFuture = httpClient.execute(httpRequestBase, null);
            long waitTime = getFutureGetTimeOut();
            if (DEBUG) {
                HttpClientHelper.printDebugInfo("future waitTime :" + waitTime);
            }
            if (waitTime > 0) {
                response = asyncFuture.get(waitTime, TimeUnit.MILLISECONDS);
            } else {
                response = asyncFuture.get();
            }
            fetchResHeaders(response, resHttpHeaders);
            return EntityUtils.toString(response.getEntity());
        } finally {
            httpClient.close();
            if (DEBUG) {
                HttpClientHelper.printDebugInfo("-- http req & resp time = " + (System.currentTimeMillis() - startT) + " ms ");
            }
        }
    } catch (Exception e) {
        throw new HttpCallerException(e);
    }
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 28 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project pinpoint by naver.

the class ClosableAsyncHttpClientIT method test.

@Test
public void test() throws Exception {
    CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().useSystemProperties().build();
    httpClient.start();
    try {
        HttpPost httpRequest = new HttpPost(getCallUrl());
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("param1", "value1"));
        httpRequest.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8.name()));
        Future<HttpResponse> responseFuture = httpClient.execute(httpRequest, null);
        HttpResponse response = responseFuture.get();
        if ((response != null) && (response.getEntity() != null)) {
            EntityUtils.consume(response.getEntity());
        }
    } finally {
        httpClient.close();
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printMethod();
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", CloseableHttpAsyncClient.class.getMethod("execute", HttpUriRequest.class, FutureCallback.class)));
    final String destinationId = getHostPort();
    final String httpUrl = getCallUrl();
    verifier.verifyTrace(async(event("HTTP_CLIENT_4", Class.forName("org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl").getMethod("start"), null, null, destinationId, annotation("http.url", httpUrl), annotation("http.entity", "param1=value1")), event("ASYNC", "Asynchronous Invocation"), event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("completed", Object.class))));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("get")));
    verifier.verifyTraceCount(0);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicFuture(org.apache.http.concurrent.BasicFuture) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) Test(org.junit.Test)

Example 29 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project metrics by dropwizard.

the class InstrumentedHttpClientsTimerTest method setUp.

@Before
public void setUp() throws Exception {
    CloseableHttpAsyncClient chac = new InstrumentedNHttpClientBuilder(metricRegistry, mock(HttpClientMetricNameStrategy.class)).build();
    chac.start();
    asyncHttpClient = chac;
    Timer timer = mock(Timer.class);
    when(timer.time()).thenReturn(context);
    when(metricRegistry.timer(any())).thenReturn(timer);
}
Also used : Timer(com.codahale.metrics.Timer) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) Before(org.junit.Before)

Example 30 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project cxf by apache.

the class AsyncHTTPConduitFactory method restartReactor.

private void restartReactor() {
    CloseableHttpAsyncClient client2 = client;
    resetVars();
    shutdown(client2);
}
Also used : CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient)

Aggregations

CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)34 HttpResponse (org.apache.http.HttpResponse)18 IOException (java.io.IOException)13 HttpGet (org.apache.http.client.methods.HttpGet)12 Test (org.junit.Test)8 HttpHost (org.apache.http.HttpHost)7 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)6 Future (java.util.concurrent.Future)5 InputStreamReader (java.io.InputStreamReader)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 FutureCallback (org.apache.http.concurrent.FutureCallback)4 HttpAsyncRequestProducer (org.apache.http.nio.protocol.HttpAsyncRequestProducer)4 Before (org.junit.Before)4 URL (java.net.URL)3 HttpEntity (org.apache.http.HttpEntity)3