use of org.apache.hc.core5.function.Callback in project wiremock by wiremock.
the class WebhooksAcceptanceTest method addsRandomDelayViaJSON.
@Test
public void addsRandomDelayViaJSON() throws Exception {
client.postJson("/__admin/mappings", "{\n" + " \"request\" : {\n" + " \"urlPath\" : \"/delayed\",\n" + " \"method\" : \"POST\"\n" + " },\n" + " \"postServeActions\" : [{\n" + " \"name\" : \"webhook\",\n" + " \"parameters\" : {\n" + " \"method\" : \"GET\",\n" + " \"url\" : \"" + targetServer.baseUrl() + "/callback\",\n" + " \"delay\" : {\n" + " \"type\" : \"uniform\",\n" + " \"lower\": 500,\n" + " \"upper\": 1000\n" + " }\n" + " }\n" + " }]\n" + "}");
verify(0, postRequestedFor(anyUrl()));
client.post("/delayed", new StringEntity("", TEXT_PLAIN));
Stopwatch stopwatch = Stopwatch.createStarted();
waitForRequestToTargetServer();
stopwatch.stop();
long elapsedMilliseconds = stopwatch.elapsed(MILLISECONDS);
assertThat(elapsedMilliseconds, greaterThanOrEqualTo(500L));
assertThat(elapsedMilliseconds, lessThanOrEqualTo(1500L));
verify(1, getRequestedFor(urlEqualTo("/callback")));
}
use of org.apache.hc.core5.function.Callback in project wiremock by wiremock.
the class FailingWebhookTest method failWhenExecutingTheWebhook.
@Test
public void failWhenExecutingTheWebhook() throws Exception {
extension.stubFor(post(urlPathEqualTo("/something-async")).willReturn(aResponse().withStatus(200)).withPostServeAction("webhook", webhook().withMethod(POST).withUrl(targetServer.url("/callback")).withHeader("Content-Type", "application/json").withBody("{ \"result\": \"SUCCESS\" }")));
verify(0, postRequestedFor(anyUrl()));
client.post("/something-async", new StringEntity("", TEXT_PLAIN));
latch.await(1, SECONDS);
assertThat("No webook should have been made", latch.getCount(), is(1L));
}
use of org.apache.hc.core5.function.Callback in project skywalking-java by apache.
the class HttpAsyncClientDoExecuteInterceptor method beforeMethod.
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
AsyncResponseConsumer consumer = (AsyncResponseConsumer) allArguments[2];
HttpContext context = (HttpContext) allArguments[4];
FutureCallback callback = (FutureCallback) allArguments[5];
allArguments[2] = new AsyncResponseConsumerWrapper(consumer);
allArguments[5] = new FutureCallbackWrapper(callback);
if (ContextManager.isActive()) {
context.setAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT, ContextManager.capture());
}
}
use of org.apache.hc.core5.function.Callback 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;
}
use of org.apache.hc.core5.function.Callback in project weicoder by wdcode.
the class HttpAsyncClient method download.
/**
* 下载文件
*
* @param url get提交地址
* @param callback 回调结果
*/
public static void download(String url, final CallbackVoid<byte[]> callback) {
// 声明HttpGet对象
HttpGet get = null;
try {
// 获得HttpGet对象
get = new HttpGet(url);
get.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
// 执行get
CLIENT.execute(SimpleRequestBuilder.copy(get).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.getBodyBytes());
}
@Override
public void cancelled() {
}
});
} catch (Exception e) {
LOG.error(e);
}
}
Aggregations