List of usage examples for org.apache.http.impl.client FutureRequestExecutionService FutureRequestExecutionService
public FutureRequestExecutionService(final HttpClient httpclient, final ExecutorService executorService)
From source file:com.lxf.spider.client.ClientWithRequestFuture.java
public static void main(String[] args) throws Exception { // the simplest way to create a HttpAsyncClientWithFuture HttpClient httpclient = HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build(); ExecutorService execService = Executors.newFixedThreadPool(5); FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient, execService);/*from w w w .jav a 2 s .c o m*/ try { // Because things are asynchronous, you must provide a ResponseHandler ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() { public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // simply return true if the status was OK return response.getStatusLine().getStatusCode() == 200; } }; // Simple request ... HttpGet request1 = new HttpGet("http://google.com"); HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1, HttpClientContext.create(), handler); Boolean wasItOk1 = futureTask1.get(); System.out.println("It was ok? " + wasItOk1); // Cancel a request try { HttpGet request2 = new HttpGet("http://google.com"); HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2, HttpClientContext.create(), handler); futureTask2.cancel(true); Boolean wasItOk2 = futureTask2.get(); System.out.println("It was cancelled so it should never print this: " + wasItOk2); } catch (CancellationException e) { System.out.println("We cancelled it, so this is expected"); } // Request with a timeout HttpGet request3 = new HttpGet("http://google.com"); HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3, HttpClientContext.create(), handler); Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS); System.out.println("It was ok? " + wasItOk3); FutureCallback<Boolean> callback = new FutureCallback<Boolean>() { public void completed(Boolean result) { System.out.println("completed with " + result); } public void failed(Exception ex) { System.out.println("failed with " + ex.getMessage()); } public void cancelled() { System.out.println("cancelled"); } }; // Simple request with a callback HttpGet request4 = new HttpGet("http://google.com"); // using a null HttpContext here since it is optional // the callback will be called when the task completes, fails, or is cancelled HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4, HttpClientContext.create(), handler, callback); Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS); System.out.println("It was ok? " + wasItOk4); } finally { requestExecService.close(); } }
From source file:interoperabilite.webservice.client.ClientWithRequestFuture.java
public static void main(String[] args) throws Exception { // the simplest way to create a HttpAsyncClientWithFuture HttpClient httpclient = HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(5).build(); ExecutorService execService = Executors.newFixedThreadPool(5); FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient, execService);/*from w w w . j av a 2 s . co m*/ try { // Because things are asynchronous, you must provide a ResponseHandler ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() { @Override public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // simply return true if the status was OK return response.getStatusLine().getStatusCode() == 200; } }; // Simple request ... HttpGet request1 = new HttpGet("http://httpbin.org/get"); HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1, HttpClientContext.create(), handler); Boolean wasItOk1 = futureTask1.get(); System.out.println("It was ok? " + wasItOk1); // Cancel a request try { HttpGet request2 = new HttpGet("http://httpbin.org/get"); HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2, HttpClientContext.create(), handler); futureTask2.cancel(true); Boolean wasItOk2 = futureTask2.get(); System.out.println("It was cancelled so it should never print this: " + wasItOk2); } catch (CancellationException e) { System.out.println("We cancelled it, so this is expected"); } // Request with a timeout HttpGet request3 = new HttpGet("http://httpbin.org/get"); HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3, HttpClientContext.create(), handler); Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS); System.out.println("It was ok? " + wasItOk3); FutureCallback<Boolean> callback = new FutureCallback<Boolean>() { @Override public void completed(Boolean result) { System.out.println("completed with " + result); } @Override public void failed(Exception ex) { System.out.println("failed with " + ex.getMessage()); } @Override public void cancelled() { System.out.println("cancelled"); } }; // Simple request with a callback HttpGet request4 = new HttpGet("http://httpbin.org/get"); // using a null HttpContext here since it is optional // the callback will be called when the task completes, fails, or is cancelled HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4, HttpClientContext.create(), handler, callback); Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS); System.out.println("It was ok? " + wasItOk4); } finally { requestExecService.close(); } }
From source file:com.yahoo.yrlhaifa.liveqa.challenge.http_operation.QuestionOperationHttpRequestSender.java
public void sendRequestsAndCollectAnswers() throws QuestionOperationException, InterruptedException { if (mapParticipantToAnswer.size() > 0) { throw new QuestionOperationException("BUG: The given map from system-id to answers is not empty."); }/* ww w . j a v a 2 s. c o m*/ CloseableHttpClient httpClient = HttpClients.custom().setMaxConnPerRoute(participants.size()).build(); try { ExecutorService executor = Executors.newFixedThreadPool(participants.size()); try { FutureRequestExecutionService requestExecutor = new FutureRequestExecutionService(httpClient, executor); logger.info("Sending requests using request-executor..."); sendRequestsWithRequestExecutor(requestExecutor); logger.info("Sending requests using request-executor - done."); } finally { try { executor.shutdownNow(); } catch (RuntimeException e) { // TODO Add more error handling logger.error("Failed to shutdown executor. Program continues.", e); } } } finally { try { httpClient.close(); } catch (IOException | RuntimeException e) { // TODO Add more error handling logger.error("Failed to close HTTP client. Program continues.", e); } } // Remove those who did not finish on time, but did write results. Set<Participant> didNotSucceed = new LinkedHashSet<Participant>(); for (Participant participant : mapParticipantToAnswer.keySet()) { if (!(systemsSucceeded.contains(participant))) { didNotSucceed.add(participant); } } for (Participant toRemove : didNotSucceed) { mapParticipantToAnswer.remove(toRemove); } if (exception != null) { throw exception; } }
From source file:org.jasig.cas.util.http.SimpleHttpClientFactoryBean.java
/** * Build a {@link FutureRequestExecutionService} from the current properties and a HTTP client. * * @param httpClient the provided HTTP client * @return the built request executor service *///from w w w . j a v a 2 s .co m private FutureRequestExecutionService buildRequestExecutorService(final CloseableHttpClient httpClient) { final ExecutorService definedExecutorService; // no executor service provided -> create a default one if (this.executorService == null) { definedExecutorService = new ThreadPoolExecutor(this.threadsNumber, this.threadsNumber, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(this.queueSize)); } else { definedExecutorService = this.executorService; } return new FutureRequestExecutionService(httpClient, definedExecutorService); }