Example usage for org.apache.http.impl.nio.client CloseableHttpPipeliningClient execute

List of usage examples for org.apache.http.impl.nio.client CloseableHttpPipeliningClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client CloseableHttpPipeliningClient execute.

Prototype

@Override
    public Future<List<HttpResponse>> execute(final HttpHost target, final List<HttpRequest> requests,
            final HttpContext context, final FutureCallback<List<HttpResponse>> callback) 

Source Link

Usage

From source file:com.boonya.http.async.examples.nio.client.AsyncClientPipelinedStreaming.java

public static void main(final String[] args) throws Exception {
    CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
    try {/* w  ww.ja v a  2s.  c om*/
        httpclient.start();

        HttpHost targetHost = new HttpHost("localhost", 8080);
        HttpGet[] resquests = { new HttpGet("/docs/index.html"), new HttpGet("/docs/introduction.html"),
                new HttpGet("/docs/setup.html"), new HttpGet("/docs/config/index.html") };

        List<MyRequestProducer> requestProducers = new ArrayList<MyRequestProducer>();
        List<MyResponseConsumer> responseConsumers = new ArrayList<MyResponseConsumer>();
        for (HttpGet request : resquests) {
            requestProducers.add(new MyRequestProducer(targetHost, request));
            responseConsumers.add(new MyResponseConsumer(request));
        }

        Future<List<Boolean>> future = httpclient.execute(targetHost, requestProducers, responseConsumers,
                null);
        future.get();
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");
}

From source file:httpasync.AsyncClientPipelinedStreaming.java

public static void a() {
    long start = System.currentTimeMillis();
    CloseableHttpPipeliningClient httpclient = HttpAsyncClients.createPipelining();
    try {/*from  w ww .  jav  a2 s  .  c om*/
        httpclient.start();

        HttpHost targetHost = new HttpHost("globalquotes.xignite.com", 80);
        //            HttpGet[] resquests = {
        //                    new HttpGet("/docs/index.html"),
        //                    new HttpGet("/docs/introduction.html"),
        //                    new HttpGet("/docs/setup.html"),
        //                    new HttpGet("/docs/config/index.html")
        //            };

        List<MyRequestProducer> requestProducers = new ArrayList<MyRequestProducer>();
        List<MyResponseConsumer> responseConsumers = new ArrayList<MyResponseConsumer>();
        String[] codes = StockParser.stockCodeList.split(",");
        //            500 * 17  ~~~~~~~~~~~~~~~ Shutting down : 12660
        //              avg=17348.1

        //            400 * 21  ~~~~~~~~~~~~~~~ Shutting down : 27476
        //              avg=18923.8

        int take = 400;
        for (int i = 0; i < codes.length / take; i++) {
            String code = Arrays.asList(codes).stream().skip(i * take).limit(take)
                    .collect(Collectors.joining(","));
            HttpGet request = new HttpGet(StockParser.uri + URLEncoder.encode(code.trim()));
            requestProducers.add(new MyRequestProducer(targetHost, request));
            responseConsumers.add(new MyResponseConsumer(request));
        }
        //            System.out.println("? = "+ responseConsumers.size());
        //            for (HttpGet request: resquests) {
        //                requestProducers.add(new MyRequestProducer(targetHost, request));
        //                responseConsumers.add(new MyResponseConsumer(request));
        //            }

        Future<List<Boolean>> future = httpclient.execute(targetHost, requestProducers, responseConsumers,
                new FutureCallback<List<Boolean>>() {
                    @Override
                    public void completed(List<Boolean> result) {
                    }

                    @Override
                    public void failed(Exception ex) {
                        ex.printStackTrace();
                    }

                    @Override
                    public void cancelled() {

                    }
                });
        future.get();
        System.out.println(take + " * " + responseConsumers.size()
                + "  ~~~~~~~~~~~~~~~ Shutting down : "
                + (System.currentTimeMillis() - start));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //        System.out.println("Done");
}