Search in sources :

Example 16 with Callback

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")));
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Stopwatch(com.google.common.base.Stopwatch) Test(org.junit.jupiter.api.Test)

Example 17 with 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));
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Test(org.junit.jupiter.api.Test)

Example 18 with Callback

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());
    }
}
Also used : HttpContext(org.apache.hc.core5.http.protocol.HttpContext) AsyncResponseConsumerWrapper(org.apache.skywalking.apm.plugin.httpclient.v5.wrapper.AsyncResponseConsumerWrapper) FutureCallbackWrapper(org.apache.skywalking.apm.plugin.httpclient.v5.wrapper.FutureCallbackWrapper) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) AsyncResponseConsumer(org.apache.hc.core5.http.nio.AsyncResponseConsumer)

Example 19 with Callback

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;
}
Also used : SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 20 with Callback

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);
    }
}
Also used : HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse)

Aggregations

Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)6 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)6 CancellationException (java.util.concurrent.CancellationException)5 TimeoutException (java.util.concurrent.TimeoutException)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 ExecutionException (java.util.concurrent.ExecutionException)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)4 TimeoutValueException (org.apache.hc.core5.util.TimeoutValueException)4 Stopwatch (com.google.common.base.Stopwatch)3 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)3 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)3 ContentType (org.apache.hc.core5.http.ContentType)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2