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