HttpClient不设置超时,导致调用方一直处于等待状态

网上有些帖子说默认SO_TIMEOUT超时是60S,不准确,或者是错误的说法

jar包区分版本,不同的版本对httpCLient的实现可能是不一样的

HttpClient 4.2 不设置数据超时会一直等

 

-----------------------------------------------------------------------------------------------

 

  1.   <dependency>  
  2.             <groupId>org.apache.httpcomponents</groupId>  
  3.             <artifactId>httpclient</artifactId>  
  4.             <version>${httpclient_version}</version>  
  5.     </dependency>  

 



 

 

==========================

 

costs ms:180888returns:{"message":"HAHAHA","data":null,"success":true}
 @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("/say") @ResponseBody public AjaxResult say() { try { Thread.sleep(180 * 1000); } catch (InterruptedException e) { } return AjaxResult.success("HAHAHA"); } }

public class TestHttpClientUtils { public static void main(String[] args) { long start = System.currentTimeMillis(); try { String resp = HttpClientUtilsV2.doGet("http://127.0.0.1:88/hello/say.html"); long end = System.currentTimeMillis(); System.out.println("\n\n\ncosts ms:" + (end - start) + "returns:" + resp); } catch (IOException e) { long end = System.currentTimeMillis(); System.out.println("ERROR costs ms:" + (end - start)); } } }
 

 ============================

HTTPClient调用方在等待应答过程中,如果服务方崩溃,则调用方的IO方法会立刻感知到SOCKET出错,并抛出异常

org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:88 refused

ERROR costs ms:13067

 

猜你喜欢

转载自curious.iteye.com/blog/2308271