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

The following examples show how to use org.apache.http.nio.protocol.HttpAsyncResponseConsumer. 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: 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 #2
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 #3
Source File: StreamingReadAsyncClient.java    From amazon-kinesis-video-streams-producer-sdk-java with Apache License 2.0 5 votes vote down vote up
private StreamingReadAsyncClient(final URI uri, final KinesisVideoSigner signer, final String inputInJson, final Integer connectionTimeoutInMillis, final Integer readTimeoutInMillis, final HttpAsyncResponseConsumer<HttpResponse> httpAsyncResponseConsumer, final FutureCallback<HttpResponse> futureCallback) {
    this.uri = uri;
    this.signer = signer;
    this.inputInJson = inputInJson;
    this.connectionTimeoutInMillis = connectionTimeoutInMillis;
    this.readTimeoutInMillis = readTimeoutInMillis;
    this.httpAsyncResponseConsumer = httpAsyncResponseConsumer;
    this.futureCallback = futureCallback;
    this.asyncClient = getHttpClient();
}
 
Example #4
Source File: ApacheAsyncHttpClientIT.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public List<HttpAsyncResponseConsumer> makeConsumers(List<HttpAsyncResponseConsumer> list) {

    List<HttpAsyncResponseConsumer> ls = new ArrayList<HttpAsyncResponseConsumer>();

    for (HttpAsyncResponseConsumer r : list) {
        ls.add(makeConsumer(r));
    }

    return ls;
}
 
Example #5
Source File: HttpAsyncClientInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    HttpAsyncResponseConsumer consumer = (HttpAsyncResponseConsumer) allArguments[1];
    HttpContext context = (HttpContext) allArguments[2];
    FutureCallback callback = (FutureCallback) allArguments[3];
    allArguments[1] = new HttpAsyncResponseConsumerWrapper(consumer);
    allArguments[3] = new FutureCallbackWrapper(callback);
    CONTEXT_LOCAL.set(context);
}
 
Example #6
Source File: PoolingAsyncResponseConsumerFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void producesPoolingAsyncResponseConsumer() {

    // given
    PoolingAsyncResponseConsumerFactory factory = createAsyncResponseConsumerFactory();

    // when
    HttpAsyncResponseConsumer result = factory.create();

    // then
    assertTrue(result instanceof PoolingAsyncResponseConsumer);
}
 
Example #7
Source File: TracingHttpAsyncClientBuilder.java    From brave with Apache License 2.0 4 votes vote down vote up
TracingAsyncResponseConsumer(HttpAsyncResponseConsumer<T> responseConsumer,
  HttpContext context) {
  this.responseConsumer = responseConsumer;
  this.context = context;
}
 
Example #8
Source File: KinesisVideoApacheHttpAsyncClient.java    From amazon-kinesis-video-streams-producer-sdk-java with Apache License 2.0 4 votes vote down vote up
public Builder withHttpAsyncResponseConsumer(final HttpAsyncResponseConsumer<HttpResponse> 
                  httpAsyncResponseConsumer) {
    mHttpAsyncResponseConsumer = httpAsyncResponseConsumer;
    return this;
}
 
Example #9
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 #10
Source File: HttpAsyncResponseConsumerWrapper.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public HttpAsyncResponseConsumerWrapper(HttpAsyncResponseConsumer<T> consumer) {
    this.consumer = consumer;
}
 
Example #11
Source File: LogbookHttpAsyncResponseConsumerTest.java    From logbook with MIT License 4 votes vote down vote up
@Test
void shouldWrapIOException() throws IOException {
    final HttpAsyncResponseConsumer<HttpResponse> unit = new LogbookHttpAsyncResponseConsumer<>(createConsumer());

    final BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(Attributes.STAGE, stage);

    final ResponseWritingStage last = mock(ResponseWritingStage.class);

    when(stage.process(any())).thenReturn(last);

    doThrow(new IOException()).when(last).write();

    assertThrows(UncheckedIOException.class, () ->
            unit.responseCompleted(context));
}
 
Example #12
Source File: ForwardingHttpAsyncResponseConsumerTest.java    From logbook with MIT License 4 votes vote down vote up
@Override
protected HttpAsyncResponseConsumer delegate() {
    return delegate;
}
 
Example #13
Source File: LogbookHttpAsyncResponseConsumer.java    From logbook with MIT License 4 votes vote down vote up
@Override
protected HttpAsyncResponseConsumer<T> delegate() {
    return consumer;
}
 
Example #14
Source File: LogbookHttpAsyncResponseConsumer.java    From logbook with MIT License 4 votes vote down vote up
public LogbookHttpAsyncResponseConsumer(final HttpAsyncResponseConsumer<T> consumer) {
    this.consumer = consumer;
}
 
Example #15
Source File: PoolingAsyncResponseConsumerFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public HttpAsyncResponseConsumer<HttpResponse> create() {
    return new PoolingAsyncResponseConsumer(pool);
}
 
Example #16
Source File: HttpFuture.java    From Tenable.io-SDK-for-Java with MIT License 2 votes vote down vote up
/**
 * Instantiates a new Http future.
 *
 * @param asyncHttpService   async http service instance
 * @param httpUriRequest     the http uri request
 * @param responseConsumer   the response consumer
 * @param httpResponseFuture the http response future
 * @param body               the body
 */
public HttpFuture( AsyncHttpService asyncHttpService, HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer, Future<HttpResponse> httpResponseFuture, String body ) {
    this( asyncHttpService, httpUriRequest, httpResponseFuture, body );
    this.responseConsumer = responseConsumer;
}
 
Example #17
Source File: AsyncHttpService.java    From Tenable.io-SDK-for-Java with MIT License 2 votes vote down vote up
/**
 * Retries given HTTP request. Called internally only, from the HttpFuture
 *
 * @param httpUriRequest the HttpUriRequest to retry
 * @param responseConsumer the response consumer
 * @param numRetry The retry count
 * @return the resulting Future<HttpResponse> instance
 */
Future<HttpResponse> retryOperation( HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer, int numRetry ) {
    httpUriRequest.setHeader( "X-Tio-Retry-Count", Integer.toString( numRetry ) );
    return responseConsumer == null ? asyncClient.execute( httpUriRequest, null ) : asyncClient.execute( HttpAsyncMethods.create( httpUriRequest ), responseConsumer, null, null );
}
 
Example #18
Source File: ForwardingHttpAsyncResponseConsumer.java    From logbook with MIT License votes vote down vote up
protected abstract HttpAsyncResponseConsumer<T> delegate(); 
Example #19
Source File: HttpAsyncResponseConsumerFactory.java    From log4j2-elasticsearch with Apache License 2.0 votes vote down vote up
HttpAsyncResponseConsumer<HttpResponse> create();