上周乐视网cms香港接口,数据刷新缓慢。香港运营人员一直在群里反馈tv端内容不更新。
我观察了下服务器,发现请求接口耗时很久500~6000ms。之前一般都是100ms左右。
导致很多线程一直在waiting状态,等待网络io。
所以首先找运维解决问题,原来运维人员鸟悄的给我们中港专线限速了,��汗一个。
后来运维给恢复后系统正常。
其实,看到6000ms的时候,心里咯噔一下,我记得我设置的超时时间是5000ms啊(业务需要,单条数据很大)。
原来读取超时时间没生效,只生效了连接超时时间。所以网上找了资料,介绍的很详细,试了下没问题。
尊重博主原创,特贴博客链接。copy下来只怕以后链接失效或删掉。
转自:http://blog.csdn.net/hi_kevin/article/details/32316171
HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试,在这里记录下。
测试版本为HttpClient——3.1
一:连接超时:connectionTimeout
1:指的是连接一个url的连接等待时间。
2:设置方法为:
public class TestHttpClientMain {
/**
* @param args
*/
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.executeMethod(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:测试的时候,将url改为一个不存在的url:“http://test.com”
4:超时时间3000ms过后,系统报出异常。
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(ReflectionSocketFactory.java:155)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
二:读取数据超时:soTimeout
1:指的是连接上一个url,获取response的返回等待时间
2:设置方法
public class TestHttpClientMain {
/**
* @param args
*/
public static void main(String[] args) {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(
"http://localhost:8080/firstTest.htm?method=test");
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(2000);
try {
int statusCode = client.executeMethod(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:测试的时候的连接url为我本地开启的一个url,http://localhost:8080/firstTest.htm?method=test
在我这个测试url里,当访问到这个链接时,线程sleep一段时间,来模拟返回response超时。
@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:将读取response返回超时时间设的时间比那个sleep时间短之后,运行程序给出异常:
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)
HttpClient 4 和 HttpClient 3 设置超时
HttpClient 4:
连接超时:
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);
// 或者
HttpConnectionParams.setConnectionTimeout(params,6000);
读取超时:
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);
// 或者
HttpConnectionParams.setSoTimeout(params,60000);
HttpClient 3:
连接超时:
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
读取超时:
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);
ok,以后再写httpClient这两个超时时间一定要加上,不加就很可能悲剧的了