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