public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try { httpclient.start(); Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"), new MyResponseConsumer(), null); 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"); }
@SuppressWarnings("unchecked") public void begin() throws InterruptedException { CloseableHttpAsyncClient httpclient = httpAsyncClientBuilder.build(); httpclient.start(); new Thread(() -> { while (true) { try { Url url = this.urlQueue.take(); httpclient.execute(HttpAsyncMethods.createGet(url.url), new MyResponseConsumer(url), new MyFutureCallback(url)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); }
@Override public void sendBatchActivities(List<Activity> activities) throws PXException, IOException { HttpAsyncRequestProducer producer = null; try { String requestBody = JsonUtils.writer.writeValueAsString(activities); logger.info("Sending Activity: {}", requestBody); HttpPost post = new HttpPost(this.pxConfiguration.getServerURL() + Constants.API_ACTIVITIES); post.setEntity(new StringEntity(requestBody, UTF_8)); post.setConfig(PXCommonUtils.getRequestConfig(pxConfiguration.getConnectionTimeout(),pxConfiguration.getApiTimeout())); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + pxConfiguration.getAuthToken()); producer = HttpAsyncMethods.create(post); asyncHttpClient.execute(producer, new BasicAsyncResponseConsumer(), new PxClientAsyncHandler()); } catch (Exception e) { throw new PXException(e); } finally { if (producer != null) { producer.close(); } } }
@Override public void sendEnforcerTelemetry(EnforcerTelemetry enforcerTelemetry) throws PXException, IOException{ HttpAsyncRequestProducer producer = null; try { String requestBody = JsonUtils.writer.writeValueAsString(enforcerTelemetry); logger.info("Sending enforcer telemetry: {}", requestBody); HttpPost post = new HttpPost(this.pxConfiguration.getServerURL() + Constants.API_ENFORCER_TELEMETRY); post.setEntity(new StringEntity(requestBody, UTF_8)); PXCommonUtils.getDefaultHeaders(pxConfiguration.getAuthToken()); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + pxConfiguration.getAuthToken()); post.setConfig(PXCommonUtils.getRequestConfig(pxConfiguration.getConnectionTimeout(),pxConfiguration.getApiTimeout())); producer = HttpAsyncMethods.create(post); asyncHttpClient.execute(producer, new BasicAsyncResponseConsumer(), new PxClientAsyncHandler()); } catch (Exception e) { e.printStackTrace(); } finally { if (producer != null) { producer.close(); } } }
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try { httpclient.start(); Future<Boolean> future = httpclient.execute( HttpAsyncMethods.createGet("http://localhost:8080/"), new MyResponseConsumer(), null); 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"); }
private Future<Response> executeRequest(Request request, HttpRequestBase method, HttpContext context, HTTPCallback<HttpResponse> callback) { if (request.isDownload()) { HttpAsyncRequestProducer producer = HttpAsyncMethods.create(method); HttpAsyncResponseConsumer<HttpResponse> consumer = new BasicAsyncResponseConsumer(); return executeRequest(producer, consumer, context, callback); } else return executeRequest(method, context, callback); }
/** * Send HttpRequest to Client * @param uri - FPC Client Uri */ public void connectToClient(String uri){ this.clientUri = uri; try{ client.start(); HttpAsyncRequestProducer get = HttpAsyncMethods.createGet(this.clientUri); client.execute(get, new MyResponseConsumer(this.clientUri), null); } catch (Exception e) { ErrorLog.logError(e.getStackTrace()); } }
public static Func1<String, Observable<ObservableHttpResponse>> postJSON(final String baseURL, final CloseableHttpAsyncClient httpClient) { final URI baseURI = URI.create(baseURL); return new Func1<String, Observable<ObservableHttpResponse>>() { @Override public Observable<ObservableHttpResponse> call(String json) { try { return ObservableHttp.createRequest(HttpAsyncMethods.createPost(baseURI, json, ContentType.APPLICATION_JSON), httpClient).toObservable(); } catch (UnsupportedEncodingException e) { throw Exceptions.propagate(e); } } }; }
/** * Retries given HTTP request. Called internally only, from the HttpFuture * * @param httpUriRequest the HttpUriRequest to retry * @param responseConsumer the response consumer * @return the resulting Future<HttpResponse> instance */ Future<HttpResponse> retryOperation( HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer ) { return responseConsumer == null ? asyncClient.execute( httpUriRequest, null ) : asyncClient.execute( HttpAsyncMethods.create( httpUriRequest ), responseConsumer, null, null ); }