List of usage examples for org.apache.http.impl.nio.client CloseableHttpAsyncClient isRunning
public abstract boolean isRunning();
From source file:org.wisdom.test.http.HttpClientHelper.java
/** * Emits an asynchronous request.//w w w .j ava2 s . c o m * * @param request the request * @param responseClass the response class * @param callback the completion callback * @param <T> the type of the expected result * @return the future to retrieve the result */ public static <T> Future<HttpResponse<T>> requestAsync(HttpRequest request, final Class<T> responseClass, Callback<T> callback) { HttpUriRequest requestObj = prepareRequest(request); CloseableHttpAsyncClient asyncHttpClient = ClientFactory.getAsyncHttpClient(); if (!asyncHttpClient.isRunning()) { asyncHttpClient.start(); } final Future<org.apache.http.HttpResponse> future = asyncHttpClient.execute(requestObj, prepareCallback(responseClass, callback)); return new Future<HttpResponse<T>>() { /** * Cancels the request. * * @param mayInterruptIfRunning whether or not we need to interrupt the request. * @return {@literal true} if the task is successfully canceled. */ public boolean cancel(boolean mayInterruptIfRunning) { return future.cancel(mayInterruptIfRunning); } /** * @return whether the future is cancelled. */ public boolean isCancelled() { return future.isCancelled(); } /** * @return whether the result is available. */ public boolean isDone() { return future.isDone(); } /** * Gets the result. * @return the response. * @throws InterruptedException if the request is interrupted. * @throws ExecutionException if the request fails. */ public HttpResponse<T> get() throws InterruptedException, ExecutionException { org.apache.http.HttpResponse httpResponse = future.get(); return new HttpResponse<>(httpResponse, responseClass); } /** * Gets the result. * @param timeout timeout configuration * @param unit unit timeout * @return the response. * @throws InterruptedException if the request is interrupted. * @throws ExecutionException if the request fails. * @throws TimeoutException if the set time out is reached before the completion of the request. */ public HttpResponse<T> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { org.apache.http.HttpResponse httpResponse = future.get(timeout * TimeUtils.TIME_FACTOR, unit); return new HttpResponse<>(httpResponse, responseClass); } }; }
From source file:co.paralleluniverse.fibers.dropwizard.InstrumentedNHttpClientBuilder.java
@Override public CloseableHttpAsyncClient build() { final CloseableHttpAsyncClient ac = super.build(); return new CloseableHttpAsyncClient() { @Override//from w w w. j av a 2 s. c o m 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(); } }; }
From source file:net.ychron.unirestinst.http.UnirestInst.java
/** * Close the asynchronous client and its event loop. Use this method to close all the threads and allow an application to exit. *///from ww w.j a va 2 s .c om public void shutdown() throws IOException { // Closing the Sync HTTP client CloseableHttpClient syncClient = (CloseableHttpClient) options.getOption(Option.HTTPCLIENT); if (syncClient != null) { syncClient.close(); } SyncIdleConnectionMonitorThread syncIdleConnectionMonitorThread = (SyncIdleConnectionMonitorThread) options .getOption(Option.SYNC_MONITOR); if (syncIdleConnectionMonitorThread != null) { syncIdleConnectionMonitorThread.interrupt(); } // Closing the Async HTTP client (if running) CloseableHttpAsyncClient asyncClient = (CloseableHttpAsyncClient) options.getOption(Option.ASYNCHTTPCLIENT); if (asyncClient != null && asyncClient.isRunning()) { asyncClient.close(); } AsyncIdleConnectionMonitorThread asyncMonitorThread = (AsyncIdleConnectionMonitorThread) options .getOption(Option.ASYNC_MONITOR); if (asyncMonitorThread != null) { asyncMonitorThread.interrupt(); } }
From source file:net.ychron.unirestinst.http.HttpClientHelper.java
public <T> Future<HttpResponse<T>> requestAsync(HttpRequest request, final Class<T> responseClass, Callback<T> callback) { HttpUriRequest requestObj = prepareRequest(request, true); CloseableHttpAsyncClient asyncHttpClient = options.getAsyncHttpClient(); if (!asyncHttpClient.isRunning()) { asyncHttpClient.start();//from www.java2 s . co m AsyncIdleConnectionMonitorThread asyncIdleConnectionMonitorThread = (AsyncIdleConnectionMonitorThread) options .getOption(Option.ASYNC_MONITOR); asyncIdleConnectionMonitorThread.start(); } final Future<org.apache.http.HttpResponse> future = asyncHttpClient.execute(requestObj, prepareCallback(responseClass, callback)); return new Future<HttpResponse<T>>() { public boolean cancel(boolean mayInterruptIfRunning) { return future.cancel(mayInterruptIfRunning); } public boolean isCancelled() { return future.isCancelled(); } public boolean isDone() { return future.isDone(); } public HttpResponse<T> get() throws InterruptedException, ExecutionException { org.apache.http.HttpResponse httpResponse = future.get(); return new HttpResponse<T>(options, httpResponse, responseClass); } public HttpResponse<T> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { org.apache.http.HttpResponse httpResponse = future.get(timeout, unit); return new HttpResponse<T>(options, httpResponse, responseClass); } }; }
From source file:org.jenkinsci.plugins.relution_publisher.net.RequestManager.java
private HttpResponse send(final ApiRequest request, final Log log) throws IOException, InterruptedException, ExecutionException { final CloseableHttpAsyncClient client = this.getHttpClient(); int retries = MAX_REQUEST_RETRIES; if (!client.isRunning()) { client.start();// www . ja va 2 s . co m } while (true) { try { final Future<HttpResponse> future = request.execute(client); return future.get(); } catch (final ExecutionException e) { retries = this.attemptRetryOnException(e, retries, log); } } }
From source file:co.paralleluniverse.fibers.httpclient.FiberHttpClient.java
public FiberHttpClient(CloseableHttpAsyncClient client, HttpRequestRetryHandler httpRequestRetryHandler, IOReactor ioreactor) {//from w w w . ja v a 2s .c o m this.client = client; this.httpRequestRetryHandler = httpRequestRetryHandler; if (ioreactor != null && ioreactor instanceof DefaultConnectingIOReactor) this.ioreactor = (DefaultConnectingIOReactor) ioreactor; if (!client.isRunning()) client.start(); }
From source file:com.github.avarabyeu.restendpoint.http.HttpClientRestEndpoint.java
/** * Default constructor./*from w w w.ja v a 2 s. c o m*/ * * @param httpClient Apache Async Http Client * @param serializers Serializer for converting HTTP messages. Shouldn't be null * @param errorHandler Error handler for HTTP messages * @param baseUrl REST WebService Base URL */ public HttpClientRestEndpoint(CloseableHttpAsyncClient httpClient, List<Serializer> serializers, ErrorHandler<HttpUriRequest, HttpResponse> errorHandler, String baseUrl) { Preconditions.checkArgument(null != serializers && !serializers.isEmpty(), "There is no any serializer provided"); //noinspection ConstantConditions this.serializers = ImmutableList.<Serializer>builder().addAll(serializers).add(new VoidSerializer()) .build(); if (!Strings.isNullOrEmpty(baseUrl)) { Preconditions.checkArgument(IOUtils.isValidUrl(baseUrl), "'%s' is not valid URL", baseUrl); } this.baseUrl = baseUrl; this.errorHandler = errorHandler == null ? new DefaultErrorHandler() : errorHandler; this.httpClient = httpClient; if (!httpClient.isRunning()) { httpClient.start(); } }