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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations