The difference between the three timeouts inside HttpClient

1. The difference between the three timeouts in

HttpClient There are three timeout settings in HttpClient: the connection pool obtains the available connection timeout, the connection timeout, and the read data timeout

First look
RequestConfig requestConfig = RequestConfig.custom()  
                    .setConnectionRequestTimeout(config.connReqTimeout) //Timeout for getting connections from the connection pool  
                            //Connection timeout with the server: httpclient will create an asynchronous thread to create a socket connection, where the connection timeout of the socket is set  
                    .setConnectTimeout(config.connTimeout)  
                    .setSocketTimeout(config.socketTimeout) //socket read data timeout: timeout for getting response data from the server  
                    .build();  
            httpClient = HttpClientBuilder.create()  
                    .setMaxConnTotal(config.maxConnTotal) //Maximum number of connections in the connection pool  
                            /**
                             * The maximum number of concurrent connections allocated to the same route (route).
                             * route: A route from the running environment machine to the target machine.
                             * For example, we use the implementation of HttpClient to request the resources of www.baidu.com and the resources of www.bing.com respectively, then he will generate two routes.
                             */  
                    .setMaxConnPerRoute(config.maxConnPerRoute)  
                    .setDefaultRequestConfig(requestConfig)  
                    .build();


Detailed explanation of the three timeout times:
1. Timeout for obtaining an available connection from the connection pool ConnectionRequestTimeout
In HttpClient, when a connection is to be used, try to obtain it from the connection pool. There is no idle connection), a get connection timeout exception will be thrown.


2. Connection target timeout connectionTimeout
refers to the connection timeout time of the connection target url, that is, the maximum time that the client sends a request to establish a connection with the target url. If the connection has not been established within this time range, a connectionTimeOut exception is thrown.

   For example, when testing, change the url to a non-existing url: "http://test.com",
after the timeout period of 3000ms, the system reports an exception:  
org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 3000 ms

3. Waiting for the response timeout (reading data timeout) socketTimeout After
   connecting to the previous url, get the response waiting time for the response, that is, after establishing a connection with the target url, wait for the maximum time to put the response back, in If no response is returned within the specified time, SocketTimeout is thrown.
   The connection url during the test is a url that I have opened locally, http://localhost:8080/firstTest.htm?method=test. In my test url, when this link is accessed, the thread sleeps for a period of time and comes to The mock returns the response timeout.

@RequestMapping(params = "method=test")  

2. public String testMethod(ModelMap model) {      

3. try {      

4.     Thread.sleep(10000);      

5. } catch (InterruptedException e) {      

6.     // TODO Auto-generated catch block      

7. e.printStackTrace ();      

8. }      

9.       System.out.println("call testMethod method.");      

10.       model.addAttribute("name", "test method");      

11. return "test";      

12.   }  


After setting the timeout time for the read response to be shorter than the sleep time, the running program gives an exception: java.net.SocketTimeoutException: Read timed out
http://blog.csdn.net/zhongzh86/article/details/46348933

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326436022&siteId=291194637
Recommended