Search in sources :

Example 1 with SimpleRequestProducer

use of org.apache.hc.client5.http.async.methods.SimpleRequestProducer in project easeagent by megaease.

the class HttpClient5AsyncTracingInterceptorTest method runOne.

private static ReportSpan runOne(final Consumer<FutureCallback<HttpResponse>> consumer) throws InterruptedException {
    SimpleHttpRequest simpleHttpRequest = SimpleHttpRequest.create("GET", "http://127.0.0.1:8080");
    SimpleRequestProducer simpleRequestProducer = SimpleRequestProducer.create(simpleHttpRequest);
    FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {

        @Override
        public void completed(HttpResponse httpResponse) {
        }

        @Override
        public void failed(Exception e) {
        }

        @Override
        public void cancelled() {
        }
    };
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { simpleRequestProducer, null, null, null, callback }).build();
    MockEaseAgent.cleanLastSpan();
    HttpClient5AsyncTracingInterceptor httpClient5AsyncTracingInterceptor = new HttpClient5AsyncTracingInterceptor();
    httpClient5AsyncTracingInterceptor.doBefore(methodInfo, EaseAgent.getContext());
    assertNotNull(EaseAgent.getContext().get(HttpClient5AsyncTracingInterceptor.class));
    httpClient5AsyncTracingInterceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(EaseAgent.getContext().get(HttpClient5AsyncTracingInterceptor.class));
    AtomicReference<FutureCallback<HttpResponse>> newCallBack = new AtomicReference<>();
    for (Object o : methodInfo.getArgs()) {
        if (o instanceof FutureCallback) {
            newCallBack.set((FutureCallback<HttpResponse>) o);
        }
    }
    assertNotNull(newCallBack.get());
    assertNull(MockEaseAgent.getLastSpan());
    Thread thread = new Thread(() -> consumer.accept(newCallBack.get()));
    thread.start();
    thread.join();
    return MockEaseAgent.getLastSpan();
}
Also used : SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 2 with SimpleRequestProducer

use of org.apache.hc.client5.http.async.methods.SimpleRequestProducer in project easeagent by megaease.

the class HttpClient5AsyncForwardedInterceptorTest method before.

@Test
public void before() {
    SimpleHttpRequest simpleHttpRequest = SimpleHttpRequest.create("GET", "http://127.0.0.1:8080");
    SimpleRequestProducer simpleRequestProducer = SimpleRequestProducer.create(simpleHttpRequest);
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { simpleRequestProducer }).build();
    HttpClient5AsyncForwardedInterceptor httpClientDoExecuteForwardedInterceptor = new HttpClient5AsyncForwardedInterceptor();
    Context context = EaseAgent.getContext();
    httpClientDoExecuteForwardedInterceptor.before(methodInfo, context);
    assertNull(simpleHttpRequest.getFirstHeader(TestConst.FORWARDED_NAME));
    context.put(TestConst.FORWARDED_NAME, TestConst.FORWARDED_VALUE);
    try {
        httpClientDoExecuteForwardedInterceptor.before(methodInfo, context);
        assertNotNull(simpleHttpRequest.getFirstHeader(TestConst.FORWARDED_NAME));
        assertEquals(TestConst.FORWARDED_VALUE, simpleHttpRequest.getFirstHeader(TestConst.FORWARDED_NAME).getValue());
    } finally {
        context.remove(TestConst.FORWARDED_NAME);
    }
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 3 with SimpleRequestProducer

use of org.apache.hc.client5.http.async.methods.SimpleRequestProducer in project invesdwin-util by invesdwin.

the class URIsConnectApacheAsync method openConnection.

public <T> Future<T> openConnection(final String method, final AsyncResponseConsumer<T> responseConsumer, final FutureCallback<T> callback) {
    final SimpleHttpRequest request = SimpleHttpRequests.create(method, uri);
    request.setConfig(getRequestConfig());
    if (headers != null) {
        for (final Entry<String, String> header : headers.entrySet()) {
            request.addHeader(header.getKey(), header.getValue());
        }
    }
    if (body != null) {
        final ContentType commonsContentType;
        if (contentType != null) {
            commonsContentType = ContentType.create(contentType);
        } else {
            commonsContentType = null;
        }
        request.setBody(body, commonsContentType);
    }
    final SimpleRequestProducer requestProducer = SimpleRequestProducer.create(request);
    final Future<T> response = getHttpClient().execute(requestProducer, responseConsumer, callback);
    return response;
}
Also used : SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) ContentType(org.apache.hc.core5.http.ContentType) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest)

Aggregations

SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)3 SimpleRequestProducer (org.apache.hc.client5.http.async.methods.SimpleRequestProducer)3 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)2 Context (com.megaease.easeagent.plugin.api.Context)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1 ContentType (org.apache.hc.core5.http.ContentType)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1 Test (org.junit.Test)1