@Test public void testRequestTimeoutIsCaught() { stubFor(get(urlEqualTo("/my/resource")) .willReturn(aResponse().withFixedDelay(3000) .withStatus(200) .withHeader("Content-Type", "text/xml") .withBody("<response>Some content</response>"))); HttpResult httpResult = AHCRequestExecutor.executeSearch(client, HttpMethod.GET,"http: assertTrue(httpResult.getStatus()== HttpSearchExecutionStatus.REQUEST_TIMEOUT); }
public static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host, String path, String searchQuery) { RequestBuilder requestBuilder = new RequestBuilder(method.name()); log.debug("Executing request against host {} with path {}",host,path); if(searchQuery!=null) { requestBuilder.setBody(searchQuery); } requestBuilder.setUrl(host + path); try { Response res = client.executeRequest(requestBuilder.build(),new AsyncCompletionHandlerBase()).get(); return new HttpResult(HttpSearchExecutionStatus.OK,res.getResponseBody()); } catch (Exception e) { Throwable cause = e.getCause(); if(cause!=null) { if(cause instanceof ConnectException) { log.error("Unable to connect to {}",host,e); return HttpResult.CONNECTION_FAILURE; } else if (cause instanceof TimeoutException) { log.error("Request timeout talking to {}",host,e); return HttpResult.REQUEST_TIMEOUT_FAILURE; } else if (cause instanceof IOException && cause.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } else { if (e instanceof IOException && e.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } } }
AHCRequestExecutor { public static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host, String path, String searchQuery) { RequestBuilder requestBuilder = new RequestBuilder(method.name()); log.debug("Executing request against host {} with path {}",host,path); if(searchQuery!=null) { requestBuilder.setBody(searchQuery); } requestBuilder.setUrl(host + path); try { Response res = client.executeRequest(requestBuilder.build(),new AsyncCompletionHandlerBase()).get(); return new HttpResult(HttpSearchExecutionStatus.OK,res.getResponseBody()); } catch (Exception e) { Throwable cause = e.getCause(); if(cause!=null) { if(cause instanceof ConnectException) { log.error("Unable to connect to {}",host,e); return HttpResult.CONNECTION_FAILURE; } else if (cause instanceof TimeoutException) { log.error("Request timeout talking to {}",host,e); return HttpResult.REQUEST_TIMEOUT_FAILURE; } else if (cause instanceof IOException && cause.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } else { if (e instanceof IOException && e.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } } } }
AHCRequestExecutor { public static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host, String path, String searchQuery) { RequestBuilder requestBuilder = new RequestBuilder(method.name()); log.debug("Executing request against host {} with path {}",host,path); if(searchQuery!=null) { requestBuilder.setBody(searchQuery); } requestBuilder.setUrl(host + path); try { Response res = client.executeRequest(requestBuilder.build(),new AsyncCompletionHandlerBase()).get(); return new HttpResult(HttpSearchExecutionStatus.OK,res.getResponseBody()); } catch (Exception e) { Throwable cause = e.getCause(); if(cause!=null) { if(cause instanceof ConnectException) { log.error("Unable to connect to {}",host,e); return HttpResult.CONNECTION_FAILURE; } else if (cause instanceof TimeoutException) { log.error("Request timeout talking to {}",host,e); return HttpResult.REQUEST_TIMEOUT_FAILURE; } else if (cause instanceof IOException && cause.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } else { if (e instanceof IOException && e.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } } }  }
AHCRequestExecutor { public static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host, String path, String searchQuery) { RequestBuilder requestBuilder = new RequestBuilder(method.name()); log.debug("Executing request against host {} with path {}",host,path); if(searchQuery!=null) { requestBuilder.setBody(searchQuery); } requestBuilder.setUrl(host + path); try { Response res = client.executeRequest(requestBuilder.build(),new AsyncCompletionHandlerBase()).get(); return new HttpResult(HttpSearchExecutionStatus.OK,res.getResponseBody()); } catch (Exception e) { Throwable cause = e.getCause(); if(cause!=null) { if(cause instanceof ConnectException) { log.error("Unable to connect to {}",host,e); return HttpResult.CONNECTION_FAILURE; } else if (cause instanceof TimeoutException) { log.error("Request timeout talking to {}",host,e); return HttpResult.REQUEST_TIMEOUT_FAILURE; } else if (cause instanceof IOException && cause.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } else { if (e instanceof IOException && e.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } } }  static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host,
                                     String path, String searchQuery); }
AHCRequestExecutor { public static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host, String path, String searchQuery) { RequestBuilder requestBuilder = new RequestBuilder(method.name()); log.debug("Executing request against host {} with path {}",host,path); if(searchQuery!=null) { requestBuilder.setBody(searchQuery); } requestBuilder.setUrl(host + path); try { Response res = client.executeRequest(requestBuilder.build(),new AsyncCompletionHandlerBase()).get(); return new HttpResult(HttpSearchExecutionStatus.OK,res.getResponseBody()); } catch (Exception e) { Throwable cause = e.getCause(); if(cause!=null) { if(cause instanceof ConnectException) { log.error("Unable to connect to {}",host,e); return HttpResult.CONNECTION_FAILURE; } else if (cause instanceof TimeoutException) { log.error("Request timeout talking to {}",host,e); return HttpResult.REQUEST_TIMEOUT_FAILURE; } else if (cause instanceof IOException && cause.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } else { if (e instanceof IOException && e.getMessage().equalsIgnoreCase("closed")) { log.warn("Unable to use client, client is closed"); return HttpResult.CLIENT_CLOSED; } else { log.error("Exception talking to {}",host,e); return new HttpResult(HttpSearchExecutionStatus.REQUEST_FAILURE,null); } } } }  static HttpResult executeSearch(AsyncHttpClient client, HttpMethod method, String host,
                                     String path, String searchQuery);  }