Examples of CloseableHttpAsyncClient


Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

                TransportAuthenticationContext context = new TransportAuthenticationContext();
                context.addAttribute("endpoint", endpoint);
                credentialsProvider = authenticationProvider.authenticate(credentialsProvider, context);

                final CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .build();

                HttpGet get = new HttpGet(endpoint.getUri());
                HttpHost target = URIUtils.extractHost(get.getURI());
                BasicAsyncRequestProducer basicAsyncRequestProducer = new BasicAsyncRequestProducer(target, get);
                httpClient.start();
                try {
                    log.debug("sending request");
                    Future<HttpResponse> futureResponse = httpClient.execute(
                            basicAsyncRequestProducer,
                            new SSEResponseConsumer(requestHandler), new FutureCallback<HttpResponse>() {
                                public void completed(HttpResponse httpResponse) {
                                    log.debug("response received {}", httpResponse);
                                }

                                public void failed(Exception e) {
                                    log.warn("failed request {}", e.toString());
                                }

                                public void cancelled() {
                                    log.warn("request cancelled");
                                }
                            });
                    requests.put(requestHandler.toString(), futureResponse);
                    futureResponse.get();

                } catch (Exception e) {
                    log.warn("cannot communicate with {}", endpoint, e);
                }
                httpClient.close();
                log.debug("request finished");
            } catch (Exception e) {
                log.error("cannot run event based replication {}", e);
            }
        }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

   
    private  HttpAsyncClient hac;

    @Before
    public void setUp() throws Exception {
        CloseableHttpAsyncClient chac = new InstrumentedNHttpClientBuilder(metricRegistry, metricNameStrategy).build();
        chac.start();
        hac = chac;
        metricRegistry.addListener(registryListener);
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

        return metricRegistry.timer(metricNameStrategy.getNameFor(name, request));
    }

    @Override
    public CloseableHttpAsyncClient build() {
        final CloseableHttpAsyncClient ac = super.build();
        return new CloseableHttpAsyncClient() {

            @Override
            public boolean isRunning() {
                return ac.isRunning();
            }

            @Override
            public void start() {
                ac.start();
            }

            @Override
            public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer, HttpAsyncResponseConsumer<T> responseConsumer, HttpContext context, FutureCallback<T> callback) {
                final Timer.Context timerContext;
                try {
                    timerContext = timer(requestProducer.generateRequest()).time();
                } catch (IOException ex) {
                    throw new AssertionError(ex);
                } catch (HttpException ex) {
                    throw new AssertionError(ex);
                }
                try {
                    return ac.execute(requestProducer, responseConsumer, context, callback);
                } finally {
                    timerContext.stop();
                }
            }

            @Override
            public void close() throws IOException {
                ac.close();
            }
        };
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

    @Test
    public void testAsync() throws IOException, InterruptedException, ExecutionException, TimeoutException {
        final int concurrencyLevel = 20;
        // snippet client configuration
        final CloseableHttpAsyncClient client = FiberCloseableHttpAsyncClient.wrap(HttpAsyncClients.
                custom().
                setMaxConnPerRoute(concurrencyLevel).
                setMaxConnTotal(concurrencyLevel).
                build());
        client.start();
        // end of snippet
        new Fiber<Void>(new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                try {
                    // snippet future calls
                    ArrayList<Future<HttpResponse>> futures = new ArrayList<>();
                    for (int i = 0; i < concurrencyLevel; i++)
                        futures.add(client.execute(new HttpGet("http://localhost:8080"), null));
                    for (Future<HttpResponse> future : futures)
                        assertEquals("testGet", EntityUtils.toString(future.get().getEntity()));
                    // end of snippet
                } catch (ExecutionException | IOException | ParseException ex) {
                    fail(ex.getMessage());
                }
            }
        }).start().join(5000, TimeUnit.MILLISECONDS);
        client.close();
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

    }

    @Override
    public CloseableHttpAsyncClient build() {

        final CloseableHttpAsyncClient ac = super.build();
        return new CloseableHttpAsyncClient() {

            @Override
            public boolean isRunning() {
                return ac.isRunning();
            }

            @Override
            public void start() {
                ac.start();
            }

            @Override
            public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer, HttpAsyncResponseConsumer<T> responseConsumer, HttpContext context, FutureCallback<T> callback) {
                final Timer.Context timerContext;
                try {
                    timerContext = timer(requestProducer.generateRequest()).time();
                } catch (IOException | HttpException ex) {
                    throw new AssertionError();
                }
                try {
                    return ac.execute(requestProducer, responseConsumer, context, callback);
                } finally {
                    timerContext.stop();
                }
            }

            @Override
            public void close() throws IOException {
                ac.close();
            }
        };
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

        final NHttpClientConnectionManager manager = createConnectionManager(registry, name);
        HttpAsyncClientBuilder clientBuilder = new InstrumentedNHttpClientBuilder(metricRegistry, name);
        clientBuilder.setConnectionManager(manager);
        clientBuilder.setDefaultRequestConfig(createHttpParams);
        setStrategiesForClient(clientBuilder);
        CloseableHttpAsyncClient client = clientBuilder.build();
        client.start();
        return new FiberHttpClient(client, getRetryHandler());
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

    public static List<Response> fetchUrls(List<String> urlStrings, int attemptCount) {
        if (attemptCount < 1) {
            throw new IllegalArgumentException("Argument \'attemptCount\' can't be less than \'1\'.");
        }

        CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
        client.start();


        try {
            int urlStringCount = urlStrings.size();
            String[] urlStringsInternal = new String[urlStrings.size()];
            urlStrings.toArray(urlStringsInternal);

            Response[] responses = new Response[urlStringCount];
            @SuppressWarnings("unchecked") Future<HttpResponse>[] futureResponses = new Future[urlStringCount];

            for (int attemptIndex = 0; attemptIndex < attemptCount; ++attemptIndex) {
                fetchUrlsInternal(client, urlStringCount, urlStringsInternal, responses, futureResponses);
            }

            return Arrays.asList(responses);
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                // No operations.
            }
        }
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

    RequestConfig requestConfig = RequestConfig.custom().build();
    HttpAsyncClientBuilder builder = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig);
    // default is 2 per host which won't let us monitor multiple directories
    builder.setMaxConnPerRoute(Integer.MAX_VALUE);
    builder.setMaxConnTotal(Integer.MAX_VALUE);
    CloseableHttpAsyncClient httpClient = builder.build();
    httpClient.start();
    return httpClient;
  }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

import org.apache.http.impl.nio.client.HttpAsyncClients;

public class AsyncClientHttpExchange {

    public static void main(final String[] args) throws Exception {
        final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();
        try {
            final HttpGet request = new HttpGet("http://www.apache.org/");
            final Future<HttpResponse> future = httpclient.execute(request, null);
            final HttpResponse response = future.get();
            System.out.println("Response: " + response.getStatusLine());
            System.out.println("Shutting down");
        } finally {
            httpclient.close();
        }
        System.out.println("Done");
    }
View Full Code Here

Examples of org.apache.http.impl.nio.client.CloseableHttpAsyncClient

import org.apache.http.protocol.HttpContext;

public class AsyncClientHttpExchangeStreaming {

    public static void main(final String[] args) throws Exception {
        final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();
        try {
            final Future<Boolean> future = httpclient.execute(
                    HttpAsyncMethods.createGet("http://localhost:8080/"),
                    new MyResponseConsumer(), null);
            final Boolean result = future.get();
            if (result != null && result.booleanValue()) {
                System.out.println("Request successfully executed");
            } else {
                System.out.println("Request failed");
            }
            System.out.println("Shutting down");
        } finally {
            httpclient.close();
        }
        System.out.println("Done");
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.