Search in sources :

Example 1 with BasicClientExchangeHandler

use of org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler in project httpcomponents-core by apache.

the class H2MultiplexingRequester method execute.

public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
    Args.notNull(requestProducer, "Request producer");
    Args.notNull(responseConsumer, "Response consumer");
    Args.notNull(timeout, "Timeout");
    final ComplexFuture<T> future = new ComplexFuture<>(callback);
    final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {

        @Override
        public void completed(final T result) {
            future.completed(result);
        }
    });
    execute(exchangeHandler, pushHandlerFactory, future, timeout, context != null ? context : HttpCoreContext.create());
    return future;
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicClientExchangeHandler(org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler) ComplexFuture(org.apache.hc.core5.concurrent.ComplexFuture)

Example 2 with BasicClientExchangeHandler

use of org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler in project httpcomponents-core by apache.

the class HttpAsyncRequester method execute.

public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
    Args.notNull(requestProducer, "Request producer");
    Args.notNull(responseConsumer, "Response consumer");
    Args.notNull(timeout, "Timeout");
    final BasicFuture<T> future = new BasicFuture<>(callback);
    final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {

        @Override
        public void completed(final T result) {
            future.completed(result);
        }
    });
    execute(exchangeHandler, pushHandlerFactory, timeout, context != null ? context : HttpCoreContext.create());
    return future;
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicFuture(org.apache.hc.core5.concurrent.BasicFuture) BasicClientExchangeHandler(org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler)

Example 3 with BasicClientExchangeHandler

use of org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler in project httpcomponents-core by apache.

the class H2ServerAndMultiplexingRequesterTest method testMultiplexedRequestCancellation.

@Test
public void testMultiplexedRequestCancellation() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), scheme);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final int reqNo = 20;
    final CountDownLatch countDownLatch = new CountDownLatch(reqNo);
    final Random random = new Random();
    final HttpHost target = new HttpHost(scheme.id, "localhost", address.getPort());
    for (int i = 0; i < reqNo; i++) {
        final Cancellable cancellable = requester.execute(new BasicClientExchangeHandler<>(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {

            @Override
            public void completed(final Message<HttpResponse, String> result) {
                countDownLatch.countDown();
            }

            @Override
            public void failed(final Exception ex) {
                countDownLatch.countDown();
            }

            @Override
            public void cancelled() {
                countDownLatch.countDown();
            }
        }), TIMEOUT, HttpCoreContext.create());
        Thread.sleep(random.nextInt(10));
        cancellable.cancel();
    }
    assertThat(countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()), CoreMatchers.equalTo(true));
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.hc.core5.concurrent.Cancellable) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) Random(java.util.Random) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) Test(org.junit.Test)

Aggregations

AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)2 BasicClientExchangeHandler (org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler)2 InetSocketAddress (java.net.InetSocketAddress)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BasicFuture (org.apache.hc.core5.concurrent.BasicFuture)1 Cancellable (org.apache.hc.core5.concurrent.Cancellable)1 ComplexFuture (org.apache.hc.core5.concurrent.ComplexFuture)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 Message (org.apache.hc.core5.http.Message)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 StringAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 BasicResponseConsumer (org.apache.hc.core5.http.nio.support.BasicResponseConsumer)1 ListenerEndpoint (org.apache.hc.core5.reactor.ListenerEndpoint)1 Test (org.junit.Test)1