@Test public void testMultiGet() { MultiGetRequest mget = new MultiGetRequest(Executors.newCachedThreadPool(), new MultiThreadedHttpConnectionManager()); List<String> urls = Arrays.asList("http: "http: "http: "http: final int requestTimeoutMs = 1000; CompletionService<GetMethod> completionService = mget.execute(urls, requestTimeoutMs); int success = 0; int errors = 0; int timeouts = 0; for (int i = 0; i < urls.size(); i++) { GetMethod getMethod = null; try { getMethod = completionService.take().get(); if (getMethod.getStatusCode() >= 300) { ++errors; Assert.assertEquals(getMethod.getResponseBodyAsString(), ERROR_MSG); } else { ++success; Assert.assertEquals(getMethod.getResponseBodyAsString(), SUCCESS_MSG); } } catch (InterruptedException e) { LOGGER.error("Interrupted", e); ++errors; } catch (ExecutionException e) { if (Throwables.getRootCause(e) instanceof SocketTimeoutException) { LOGGER.debug("Timeout"); ++timeouts; } else { LOGGER.error("Error", e); ++errors; } } catch (IOException e) { ++errors; } } Assert.assertEquals(success, 2); Assert.assertEquals(errors, 1); Assert.assertEquals(timeouts, 1); }
public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) { HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(timeoutMs); HttpClient client = new HttpClient(clientParams, _connectionManager); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor); for (String url : urls) { completionService.submit(() -> { try { GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } catch (Exception e) { LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url); throw e; } }); } return completionService; }
MultiGetRequest { public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) { HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(timeoutMs); HttpClient client = new HttpClient(clientParams, _connectionManager); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor); for (String url : urls) { completionService.submit(() -> { try { GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } catch (Exception e) { LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url); throw e; } }); } return completionService; } }
MultiGetRequest { public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) { HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(timeoutMs); HttpClient client = new HttpClient(clientParams, _connectionManager); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor); for (String url : urls) { completionService.submit(() -> { try { GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } catch (Exception e) { LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url); throw e; } }); } return completionService; } MultiGetRequest(Executor executor, HttpConnectionManager connectionManager); }
MultiGetRequest { public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) { HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(timeoutMs); HttpClient client = new HttpClient(clientParams, _connectionManager); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor); for (String url : urls) { completionService.submit(() -> { try { GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } catch (Exception e) { LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url); throw e; } }); } return completionService; } MultiGetRequest(Executor executor, HttpConnectionManager connectionManager); CompletionService<GetMethod> execute(List<String> urls, int timeoutMs); }
MultiGetRequest { public CompletionService<GetMethod> execute(List<String> urls, int timeoutMs) { HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(timeoutMs); HttpClient client = new HttpClient(clientParams, _connectionManager); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(_executor); for (String url : urls) { completionService.submit(() -> { try { GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } catch (Exception e) { LOGGER.warn("Caught '{}' while executing GET on URL: {}", e.toString(), url); throw e; } }); } return completionService; } MultiGetRequest(Executor executor, HttpConnectionManager connectionManager); CompletionService<GetMethod> execute(List<String> urls, int timeoutMs);  }