public AnalyticsConfiguration build() { if (!this.isAnlayticsEnabled) { LOGGER.info("Galileo Agent is disabled"); } else if (!PropertyUtil.notBlank(this.analyticsToken)) { isAnlayticsEnabled = false; LOGGER.error("analytics Token not set"); } else { try { String scheme = (useHttps) ? "https" : "http"; this.analyticsURI = new URIBuilder().setScheme(scheme).setHost(analyticsServerHost).setPort(Integer.parseInt(analyticsServerPort)).setPath(analyticsServerPath).build(); } catch (Exception e1) { isAnlayticsEnabled = false; LOGGER.error("failed to create Galileo URI", e1); return (config = new AnalyticsConfiguration(this)); } alfsQueueSize = 2 * alfsBatchSize; alfQueue = new ArrayBlockingQueue<ALF>(alfsQueueSize); HttpClient httpclient = HttpClientBuilder.create().setMaxConnPerRoute(connectionCount).setMaxConnTotal(connectionCount).setKeepAliveStrategy(new AnalyticsConnKeepAliveStrategy(keepAliveTime)).setRetryHandler(new AnalyticsRetryHandler(retryCount)).build(); workers = Executors.newFixedThreadPool(workerCount); requestExecService = new FutureRequestExecutionService(httpclient, workers); logConfig(); startTicker(); } return (config = new AnalyticsConfiguration(this)); }
/** * Build a {@link FutureRequestExecutionService} from the current properties and a HTTP client. * * @param httpClient the provided HTTP client * @return the built request executor service */ private FutureRequestExecutionService buildRequestExecutorService(final CloseableHttpClient httpClient) { if (this.executorService == null) { this.executorService = new ThreadPoolExecutor(this.threadsNumber, this.threadsNumber, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(this.queueSize)); } return new FutureRequestExecutionService(httpClient, this.executorService); }
/** * Build a {@link FutureRequestExecutionService} from the current properties and a HTTP client. * * @param httpClient the provided HTTP client * @return the built request executor service */ private FutureRequestExecutionService buildRequestExecutorService(final CloseableHttpClient httpClient) { if (this.executorService == null) { this.executorService = new ThreadPoolExecutor(this.threadsNumber, this.threadsNumber, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(this.queueSize)); } return new FutureRequestExecutionService(httpClient, this.executorService); }
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); 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(); } }
public FutureRequestExecutionService getFutureRequestExecutor() { return requestExecService; }
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); 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(); } }
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); 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://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>() { @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://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(); } }
/** * Instantiates a new Simple HTTP client, based on the provided inputs. * * @param acceptableCodes the acceptable codes of the client * @param httpClient the HTTP client used by the client * @param requestExecutorService the request executor service used by the client */ SimpleHttpClient(final List<Integer> acceptableCodes, final CloseableHttpClient httpClient, final FutureRequestExecutionService requestExecutorService) { this.acceptableCodes = ImmutableList.copyOf(acceptableCodes); this.httpClient = httpClient; this.requestExecutorService = requestExecutorService; }
@Override public SimpleHttpClient getObject() throws Exception { final CloseableHttpClient httpClient = buildHttpClient(); final FutureRequestExecutionService requestExecutorService = buildRequestExecutorService(httpClient); return new SimpleHttpClient(this.acceptableCodes, httpClient, requestExecutorService); }
/** * Instantiates a new Simple HTTP client, based on the provided inputs. * * @param acceptableCodes the acceptable codes of the client * @param httpClient the HTTP client used by the client * @param requestExecutorService the request executor service used by the client */ public SimpleHttpClient(final List<Integer> acceptableCodes, final CloseableHttpClient httpClient, final FutureRequestExecutionService requestExecutorService) { this.acceptableCodes = ImmutableList.copyOf(acceptableCodes); this.httpClient = httpClient; this.requestExecutorService = requestExecutorService; }
/** * Instantiates a new Simple HTTP client, based on the provided inputs. * * @param acceptableCodes the acceptable codes of the client * @param httpClient the HTTP client used by the client * @param requestExecutorService the request executor service used by the client */ SimpleHttpClient(final List<Integer> acceptableCodes, final CloseableHttpClient httpClient, final FutureRequestExecutionService requestExecutorService) { this.acceptableCodes = acceptableCodes.stream().sorted().collect(Collectors.toList()); this.httpClient = httpClient; this.requestExecutorService = requestExecutorService; }