Search in sources :

Example 6 with AsyncDataConsumer

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

the class AsyncClientInterceptors method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addRequestInterceptorFirst(new HttpRequestInterceptor() {

        private final AtomicLong count = new AtomicLong(0);

        @Override
        public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
            request.setHeader("request-id", Long.toString(count.incrementAndGet()));
        }
    }).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {

        @Override
        public void execute(final HttpRequest request, final AsyncEntityProducer requestEntityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
            final Header idHeader = request.getFirstHeader("request-id");
            if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
                final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
                final ByteBuffer content = ByteBuffer.wrap("bad luck".getBytes(StandardCharsets.US_ASCII));
                final AsyncDataConsumer asyncDataConsumer = asyncExecCallback.handleResponse(response, new BasicEntityDetails(content.remaining(), ContentType.TEXT_PLAIN));
                asyncDataConsumer.consume(content);
                asyncDataConsumer.streamEnd(null);
            } else {
                chain.proceed(request, requestEntityProducer, scope, asyncExecCallback);
            }
        }
    }).build();
    client.start();
    final String requestUri = "http://httpbin.org/get";
    for (int i = 0; i < 20; i++) {
        final SimpleHttpRequest request = SimpleRequestBuilder.get(requestUri).build();
        System.out.println("Executing request " + request);
        final Future<SimpleHttpResponse> future = client.execute(request, new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void completed(final SimpleHttpResponse response) {
                System.out.println(request + "->" + new StatusLine(response));
                System.out.println(response.getBody());
            }

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

            @Override
            public void cancelled() {
                System.out.println(request + " cancelled");
            }
        });
        future.get();
    }
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : AsyncExecCallback(org.apache.hc.client5.http.async.AsyncExecCallback) AsyncExecChain(org.apache.hc.client5.http.async.AsyncExecChain) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) HttpException(org.apache.hc.core5.http.HttpException) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) IOException(java.io.IOException) AsyncExecChainHandler(org.apache.hc.client5.http.async.AsyncExecChainHandler) ByteBuffer(java.nio.ByteBuffer) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) StatusLine(org.apache.hc.core5.http.message.StatusLine) AtomicLong(java.util.concurrent.atomic.AtomicLong) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) Header(org.apache.hc.core5.http.Header) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor)

Aggregations

HttpResponse (org.apache.hc.core5.http.HttpResponse)5 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)5 IOException (java.io.IOException)4 EntityDetails (org.apache.hc.core5.http.EntityDetails)4 Header (org.apache.hc.core5.http.Header)4 HttpException (org.apache.hc.core5.http.HttpException)4 HttpRequest (org.apache.hc.core5.http.HttpRequest)4 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)4 AsyncDataConsumer (org.apache.hc.core5.http.nio.AsyncDataConsumer)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)2 AsyncPushProducer (org.apache.hc.core5.http.nio.AsyncPushProducer)2 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)2 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)2 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)2 ResponseChannel (org.apache.hc.core5.http.nio.ResponseChannel)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1