Summary of three timeout settings in httpClient in Java

Source: Internet
Author: User
Tags readline sleep socket

ConnectTimeoutException:
This exception is thrown when an HTTP server connection times out or a valid connection that is managed by HttpConnectionManager.
 
SocketTimeoutException:
This exception occurs when the read or receive Socket times out.
 
In the HttpClient package of Apache, there are three timeout settings:

The code is as follows: Copy code

/* Obtain the connection timeout value from the connection pool */
ConnManagerParams. setTimeout (partams, 1000 );
/* Connection timeout */
HttpConnectionParams. setConnectionTimeout (params, 2000 );
/* Request Timeout */
HttpConnectionParams. setSoTimeout (partams, 4000 );

 
Set ConnectionPoolTimeout in the first line:


This defines the timeout time for retrieving connections from the connection pool managed by ConnectionManager. Set this parameter to 1 second.

The second line sets ConnectionTimeout:


This defines the timeout time for establishing a connection with the server through the network. In the Httpclient package, an asynchronous thread is used to create a socket connection with the server. This is the timeout time of the socket connection, which is set to 2 seconds.


The third line sets SocketTimeout:

This defines the timeout time for Socket data reading, that is, the time needed to wait for the response data to be obtained from the server, which is set to 4 seconds.

The above three timeouts will throw ConnectionPoolTimeoutException, ConnectionTimeoutException, and SocketTimeoutException respectively.

Example

The test version is HttpClient -- 3.1.

I. Connection timeout: connectionTimeout

1: The connection wait time for a url.

2: The setting method is as follows:

The code is as follows: Copy code

Public class TestHttpClientMain {

Public static void main (String [] args ){
HttpClient client = new HttpClient ();
HttpMethod method = new GetMethod (
Http://test.com ");
Client. getHttpConnectionManager (). getParams ()
. SetConnectionTimeout (3000 );
Client. getHttpConnectionManager (). getParams (). setSoTimeout (3000 );
Try {
Int statusCode = client.exe cuteMethod (method );
System. out. println (statusCode );
Byte [] responseBody = null;
ResponseBody = method. getResponseBody ();
String result = new String (responseBody );
System. out. println (result );
} Catch (HttpException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

3: During the test, change the url to a nonexistent url: "http://www.111cn.net"

4: The system reports an exception after the timeout value of MS.

The code is as follows: Copy code

Org. apache. commons. httpclient. ConnectTimeoutException: The host did not accept the connection within timeout of 3000 MS

At org. apache. commons. httpclient. protocol. ReflectionSocketFactory. createSocket (reflectionsocksocketfactory. java: 155)
At org. apache. commons. httpclient. protocol. DefaultProtocolSocketFactory. createSocket (defaprotoprotocolsocketfactory. java: 125)
At org. apache. commons. httpclient. HttpConnection. open (HttpConnection. java: 707)
At org.apache.commons.httpclient.HttpMethodDirector.exe cuteWithRetry (HttpMethodDirector. java: 387)
At org.apache.commons.httpclient.HttpMethodDirector.exe cuteMethod (HttpMethodDirector. java: 171)
At org.apache.commons.httpclient.HttpClient.exe cuteMethod (HttpClient. java: 397)

II. Data reading timeout: soTimeout

1: connect to the previous url to obtain the response wait time

2: setting method

The code is as follows: Copy code

Public class TestHttpClientMain {

Public static void main (String [] args ){
HttpClient client = new HttpClient ();
HttpMethod method = new GetMethod (
& Quot; http: // localhost: 8080/firstTest.htm? Method = test ");
Client. getHttpConnectionManager (). getParams ()
. SetConnectionTimeout (3000 );
Client. getHttpConnectionManager (). getParams (). setSoTimeout (2000 );
Try {
Int statusCode = client.exe cuteMethod (method );
System. out. println (statusCode );
Byte [] responseBody = null;
ResponseBody = method. getResponseBody ();
String result = new String (responseBody );
System. out. println (result );
} Catch (HttpException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

3: During the test, the connection url is a locally opened url, http: // localhost: 8080/firstTest.htm? Method = test

In my test url, when the link is accessed, the thread sleep for a period of time to simulate response timeout.

The code is as follows: Copy code

@ RequestMapping (params = "method = test") // <-- ②

Public String testMethod (ModelMap model ){
Try {
Thread. sleep (3000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("call testMethod method .");
Model. addAttribute ("name", "test method ");
Return "test ";
}

4: run the program to give an exception after the timeout time set for reading response is shorter than the sleep time:

The code is as follows: Copy code

Java.net. SocketTimeoutException: Read timed out

At java.net. SocketInputStream. socketRead0 (Native Method)
At java.net. SocketInputStream. read (Unknown Source)
At java. io. BufferedInputStream. fill (Unknown Source)
At java. io. BufferedInputStream. read (Unknown Source)
At org. apache. commons. httpclient. HttpParser. readRawLine (HttpParser. java: 78)
At org. apache. commons. httpclient. HttpParser. readLine (HttpParser. java: 106)
At org. apache. commons. httpclient. HttpConnection. readLine (HttpConnection. java: 1116)
At org. apache. commons. httpclient. HttpMethodBase. readStatusLine (HttpMethodBase. java: 1973)
At org. apache. commons. httpclient. HttpMethodBase. readResponse (HttpMethodBase. java: 1735)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.