Search in sources :

Example 6 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project mercury by yellow013.

the class AsyncClientH2FullDuplexExchange method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.FORCE_HTTP_2, H2Config.DEFAULT, null, ioReactorConfig);
    client.start();
    final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
    final BasicRequestProducer requestProducer = new BasicRequestProducer(request, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
    final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new StringAsyncEntityConsumer());
    System.out.println("Executing request " + request);
    final CountDownLatch latch = new CountDownLatch(1);
    client.execute(new AsyncClientExchangeHandler() {

        @Override
        public void releaseResources() {
            requestProducer.releaseResources();
            responseConsumer.releaseResources();
            latch.countDown();
        }

        @Override
        public void cancel() {
            System.out.println(request + " cancelled");
        }

        @Override
        public void failed(final Exception cause) {
            System.out.println(request + "->" + cause);
        }

        @Override
        public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
            requestProducer.sendRequest(channel, context);
        }

        @Override
        public int available() {
            return requestProducer.available();
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            requestProducer.produce(channel);
        }

        @Override
        public void consumeInformation(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
        }

        @Override
        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
            responseConsumer.consumeResponse(response, entityDetails, context, null);
        }

        @Override
        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
            responseConsumer.updateCapacity(capacityChannel);
        }

        @Override
        public void consume(final ByteBuffer src) throws IOException {
            responseConsumer.consume(src);
        }

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            responseConsumer.streamEnd(trailers);
        }
    });
    latch.await(1, TimeUnit.MINUTES);
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) MinimalHttpAsyncClient(org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Aggregations

DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)6 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)4 IOException (java.io.IOException)3 HttpException (org.apache.hc.core5.http.HttpException)3 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)3 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)3 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)3 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)3 BasicAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer)3 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)3 Test (org.junit.jupiter.api.Test)3 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 MinimalHttpAsyncClient (org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient)2 EntityDetails (org.apache.hc.core5.http.EntityDetails)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 StatusLine (org.apache.hc.core5.http.message.StatusLine)2 AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)2 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2