org.apache.http.nio.protocol.HttpAsyncRequestProducer Java Examples

The following examples show how to use org.apache.http.nio.protocol.HttpAsyncRequestProducer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApacheHttpAsyncClientInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(value = 0, readOnly = false) HttpAsyncRequestProducer requestProducer,
                                    @Advice.Argument(2) HttpContext context,
                                    @Advice.Argument(value = 3, readOnly = false) FutureCallback futureCallback,
                                    @Advice.Local("span") @Nullable Span span,
                                    @Advice.Local("wrapped") boolean wrapped) {
    if (tracer == null || tracer.getActive() == null) {
        return;
    }
    final AbstractSpan<?> parent = tracer.getActive();
    span = parent.createExitSpan();
    if (span != null) {
        span.withType(HttpClientHelper.EXTERNAL_TYPE)
            .withSubtype(HttpClientHelper.HTTP_SUBTYPE)
            .activate();

        ApacheHttpAsyncClientHelper<HttpAsyncRequestProducer, FutureCallback<?>, HttpContext, HttpRequest> asyncHelper =
            asyncHelperManager.getForClassLoaderOfClass(HttpAsyncRequestProducer.class);
        TextHeaderSetter<HttpRequest> headerSetter = headerSetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class);
        if (asyncHelper != null && headerSetter != null) {
            requestProducer = asyncHelper.wrapRequestProducer(requestProducer, span, headerSetter);
            futureCallback = asyncHelper.wrapFutureCallback(futureCallback, context, span);
            wrapped = true;
        }
    }
}
 
Example #2
Source File: HttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncResponseIsNotPooledIfPoolNotConfigured() {

    // given
    HCHttp.Builder testObjectFactoryBuilder =
            HCHttpTest.createDefaultHttpObjectFactoryBuilder();
    testObjectFactoryBuilder.withPooledResponseBuffers(false);

    HttpClient client = spy(testObjectFactoryBuilder.build().createClient());

    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    BatchRequest request = createDefaultTestBatchRequest();

    // when
    client.executeAsync(request, createMockTestResultHandler());

    // then
    verify(client).getAsyncClient();
    verify(asyncClient).execute(
            any(HttpAsyncRequestProducer.class),
            asyncConsumerCaptor.capture(),
            any(HttpContext.class),
            any(FutureCallback.class));
    assertEquals(BasicAsyncResponseConsumer.class, asyncConsumerCaptor.getValue().getClass());
}
 
Example #3
Source File: HttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToConfiguredAsyncClient() {

    // given
    HttpClient client = Mockito.spy(createDefaultTestObject());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    BatchRequest request = createDefaultTestBatchRequest();

    // when
    client.executeAsync(request, createMockTestResultHandler());

    // then
    verify(client).getAsyncClient();
    verify(asyncClient).execute(
            any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class),
            any(HttpContext.class),
            any(FutureCallback.class));

}
 
Example #4
Source File: HttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
private HCResultCallback mockHttpResponseCallback(ResponseHandler<Response> responseHandler, BatchResult batchResult) throws IOException {

        HttpClient client = Mockito.spy(createDefaultTestObject());
        CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

        BatchRequest request = mock(BatchRequest.class);
        when(request.getURI()).thenReturn(UUID.randomUUID().toString());
        when(request.getHttpMethodName()).thenReturn(BatchRequest.HTTP_METHOD_NAME);
        ItemSource itemSource = mock(ItemSource.class);
        when(itemSource.getSource()).thenReturn(mock(ByteBuf.class));
        when(request.serialize()).thenReturn(itemSource);

        when(responseHandler.deserializeResponse(any(InputStream.class)))
                .thenReturn(batchResult);

        client.executeAsync(request, responseHandler);
        verify(asyncClient).execute(
                any(HttpAsyncRequestProducer.class),
                any(PoolingAsyncResponseConsumer.class),
                any(HttpClientContext.class),
                hcResultCallbackCaptor.capture());

        return hcResultCallbackCaptor.getValue();
    }
 
Example #5
Source File: TracingHttpAsyncClientBuilder.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer,
  HttpAsyncResponseConsumer<T> responseConsumer, HttpContext httpContext,
  FutureCallback<T> callback) {

  TraceContext invocationContext = currentTraceContext.get();
  if (invocationContext != null) {
    httpContext.setAttribute(TraceContext.class.getName(), invocationContext);
  }

  return delegate.execute(
    new TracingAsyncRequestProducer(requestProducer, httpContext),
    new TracingAsyncResponseConsumer<>(responseConsumer, httpContext),
    httpContext,
    callback != null && invocationContext != null
      ? new TraceContextFutureCallback<>(callback, currentTraceContext, invocationContext)
      : callback
  );
}
 
Example #6
Source File: HttpAsyncRequestProducerWrapper.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
public HttpAsyncRequestProducerWrapper with(HttpAsyncRequestProducer delegate, Span span, TextHeaderSetter<HttpRequest> headerSetter) {
    // Order is important due to visibility - write to delegate last on this (initiating) thread
    this.span = span;
    this.headerSetter = headerSetter;
    this.delegate = delegate;
    return this;
}
 
Example #7
Source File: KinesisVideoApacheHttpAsyncClient.java    From amazon-kinesis-video-streams-producer-sdk-java with Apache License 2.0 5 votes vote down vote up
public void executeRequest() {
    final HttpPost request = new HttpPost(mBuilder.mUri);
    for (Map.Entry<String, String> entry : mBuilder.mHeaders.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());
    }
    final HttpEntity entity = new StringEntity(mBuilder.mContentInJson, mBuilder.mContentType);
    request.setEntity(entity);
    final HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(request);
    this.mHttpClient.execute(requestProducer, ((Builder) mBuilder).mHttpAsyncResponseConsumer,
            ((Builder) mBuilder).mFutureCallback);
}
 
Example #8
Source File: DefaultClientExchangeHandlerImplStartMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private HttpRequest getHttpRequest(final Object target) {
    try {
        if (!(target instanceof RequestProducerGetter)) {
            return null;
        }
        final HttpAsyncRequestProducer requestProducer = ((RequestProducerGetter) target)._$PINPOINT$_getRequestProducer();
        return requestProducer.generateRequest();
    } catch (Exception e) {
        return null;
    }
}
 
Example #9
Source File: DefaultClientExchangeHandlerImplStartMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private NameIntValuePair<String> getHost(final Object target) {
    if (target instanceof RequestProducerGetter) {
        final HttpAsyncRequestProducer producer = ((RequestProducerGetter) target)._$PINPOINT$_getRequestProducer();
        final HttpHost httpHost = producer.getTarget();
        if (httpHost != null) {
            return new NameIntValuePair<String>(httpHost.getHostName(), httpHost.getPort());
        }
    }
    return new NameIntValuePair<String>(null, -1);
}
 
Example #10
Source File: ApacheHttpAsyncClientHelperImpl.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
public HttpAsyncRequestProducer wrapRequestProducer(HttpAsyncRequestProducer requestProducer, Span span, TextHeaderSetter<HttpRequest> headerSetter) {
    return requestProducerWrapperObjectPool.createInstance().with(requestProducer, span, headerSetter);
}
 
Example #11
Source File: HttpAsyncClientInterceptorTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
private Thread baseTest() throws Throwable {
    Object[] allArguments = new Object[] {
        producer,
        consumer,
        httpContext,
        callback
    };
    Class[] types = new Class[] {
        HttpAsyncRequestProducer.class,
        HttpAsyncResponseConsumer.class,
        HttpContext.class,
        FutureCallback.class
    };
    httpAsyncClientInterceptor.beforeMethod(enhancedInstance, null, allArguments, types, null);
    Assert.assertEquals(CONTEXT_LOCAL.get(), httpContext);
    Assert.assertTrue(allArguments[1] instanceof HttpAsyncResponseConsumerWrapper);
    Assert.assertTrue(allArguments[3] instanceof FutureCallbackWrapper);

    sessionRequestConstructorInterceptor.onConstruct(enhancedInstance, null);

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //start local
                completeInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
                //start request
                requestExecutorInterceptor.beforeMethod(enhancedInstance, null, null, null, null);

                HttpAsyncResponseConsumerWrapper consumerWrapper = new HttpAsyncResponseConsumerWrapper(consumer);

                consumerWrapper.responseReceived(response);

                new FutureCallbackWrapper(callback).completed(null);

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    });
    thread.start();
    return thread;
}
 
Example #12
Source File: TracingHttpAsyncClientBuilder.java    From brave with Apache License 2.0 4 votes vote down vote up
TracingAsyncRequestProducer(HttpAsyncRequestProducer requestProducer, HttpContext context) {
  this.requestProducer = requestProducer;
  this.context = context;
}
 
Example #13
Source File: RequestProducerGetter.java    From pinpoint with Apache License 2.0 votes vote down vote up
HttpAsyncRequestProducer _$PINPOINT$_getRequestProducer();