use of org.apache.hc.core5.concurrent.FutureCallback in project metrics by dropwizard.
the class InstrumentedHttpAsyncClientsTest method registersExpectedMetricsGivenNameStrategy.
@Test
public void registersExpectedMetricsGivenNameStrategy() throws Exception {
client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();
client.start();
final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String metricName = "some.made.up.metric.name";
httpServer.createContext("/", exchange -> {
exchange.sendResponseHeaders(200, 0L);
exchange.setStreams(null, null);
exchange.getResponseBody().write("TEST".getBytes(StandardCharsets.US_ASCII));
exchange.close();
});
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse result) {
assertThat(result.getBodyText()).isEqualTo("TEST");
}
@Override
public void failed(Exception ex) {
fail();
}
@Override
public void cancelled() {
fail();
}
});
responseFuture.get(1L, TimeUnit.SECONDS);
verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
}
use of org.apache.hc.core5.concurrent.FutureCallback in project metrics by dropwizard.
the class InstrumentedHttpAsyncClientsTest method usesCustomClientConnectionManager.
@Test
public void usesCustomClientConnectionManager() throws Exception {
try (PoolingAsyncClientConnectionManager clientConnectionManager = spy(new PoolingAsyncClientConnectionManager())) {
client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy, clientConnectionManager).disableAutomaticRetries().build();
client.start();
final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String metricName = "some.made.up.metric.name";
httpServer.createContext("/", exchange -> {
exchange.sendResponseHeaders(200, 0L);
exchange.setStreams(null, null);
exchange.getResponseBody().write("TEST".getBytes(StandardCharsets.US_ASCII));
exchange.close();
});
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse result) {
assertThat(result.getCode()).isEqualTo(200);
}
@Override
public void failed(Exception ex) {
fail();
}
@Override
public void cancelled() {
fail();
}
});
responseFuture.get(1L, TimeUnit.SECONDS);
verify(clientConnectionManager, atLeastOnce()).connect(any(), any(), any(), any(), any(), any());
}
}
use of org.apache.hc.core5.concurrent.FutureCallback in project weicoder by wdcode.
the class HttpAsyncClient method post.
/**
* 模拟post提交
*
* @param url post提交地址
* @param data 提交参数
* @param callback 回调结果
* @param charset 编码
*/
public static void post(String url, Map<String, Object> data, CallbackVoid<String> callback, String charset) {
// 声明HttpPost
HttpPost post = null;
try {
// 获得HttpPost
post = new HttpPost(url);
post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
// 如果参数列表为空 data为空map
if (U.E.isNotEmpty(data)) {
// 声明参数列表
List<NameValuePair> list = Lists.newList(data.size());
// 设置参数
data.forEach((k, v) -> list.add(new BasicNameValuePair(k, W.C.toString(v))));
// 设置参数与 编码格式
post.setEntity(new UrlEncodedFormEntity(list));
}
// 执行POST
CLIENT.execute(SimpleRequestBuilder.copy(post).build(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void failed(Exception ex) {
LOG.error(ex);
}
@Override
public void completed(SimpleHttpResponse result) {
if (callback != null)
callback.callback(result.getBodyText());
}
@Override
public void cancelled() {
}
});
} catch (Exception e) {
LOG.error(e);
}
}
use of org.apache.hc.core5.concurrent.FutureCallback in project metrics by dropwizard.
the class InstrumentedHttpAsyncClientsTest method registersExpectedExceptionMetrics.
@Test
public void registersExpectedExceptionMetrics() throws Exception {
client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();
client.start();
final CountDownLatch countDownLatch = new CountDownLatch(1);
final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String requestMetricName = "request";
final String exceptionMetricName = "exception";
httpServer.createContext("/", HttpExchange::close);
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
try {
final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse result) {
fail();
}
@Override
public void failed(Exception ex) {
countDownLatch.countDown();
}
@Override
public void cancelled() {
fail();
}
});
countDownLatch.await(5, TimeUnit.SECONDS);
responseFuture.get(5, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
assertThat(e).hasCauseInstanceOf(ConnectionClosedException.class);
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(metricRegistry.getMeters()).containsKey("exception"));
}
}
use of org.apache.hc.core5.concurrent.FutureCallback in project feign by OpenFeign.
the class AsyncApacheHttp5Client method execute.
@Override
public CompletableFuture<Response> execute(Request request, Options options, Optional<HttpClientContext> requestContext) {
final SimpleHttpRequest httpUriRequest = toClassicHttpRequest(request, options);
final CompletableFuture<Response> result = new CompletableFuture<>();
final FutureCallback<SimpleHttpResponse> callback = new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse httpResponse) {
result.complete(toFeignResponse(httpResponse, request));
}
@Override
public void failed(Exception ex) {
result.completeExceptionally(ex);
}
@Override
public void cancelled() {
result.cancel(false);
}
};
client.execute(httpUriRequest, configureTimeouts(options, requestContext.orElseGet(HttpClientContext::new)), callback);
return result;
}
Aggregations