linux 上,使用HTTPClient 发送请求到 远程服务器,在1000个并发以内(包括1000) 的时候无任何异常, 但是只要超过1000, 即使是1001, 则出现问题:
INFO [08-17 15:52:12] [Thread-2024] org.apache.http.impl.client.DefaultRequestDirector.tryExecute(696) | I/O exception (java.net.SocketException) caught when processing request to {}->http://192.168.4.190:80: Broken pipe
INFO [08-17 15:52:12] [Thread-2024]
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(706) | Retrying request to {}->http://192.168.4.190:80
不知道为什么是 1000 , 不知道哪里有限制 刚好是1000 。
windows上执行同样的代码就没有这样的问题现象。这太奇怪了, 我百思不得其解。 请求各位大神看看。
java代码:
import java.io.File;
import java.util.Date;
import java.util.concurrent.ExecutionException;
public class TestHTTP {
// 线程池的容量
/**
* @param args
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
int cnt = 2001;
if (args.length > 0 ) {
cnt = StringUtil.str2Int(args[0]);
}
System.out.println(111 + new Date().toString());
for (int i = 0; i < cnt; i++) {
TTT ttt = new TTT();// f, "test"
Thread t = new Thread(ttt);
t.start();
}
}
}
class TTT extends Thread {
@Override
public void run() {
String pathname = "E:\\test\\cca.txt";
pathname = "C:\\Users\\Public\\Documents\\data.txt";
pathname = "/usr/linkapp/bin/tomcat-master/lktest/data.txt";
File f = new File(pathname);
String key = LKRestUtil.put(f);
System.out.println(" key : " + key + new Date().toString());
}
}
LKRestUtil.put 方法 :
HttpClient httpClient = getHttpClient(); // 从连接池中获取
String url = "http://"+MyNodeStatusChecker.mainHost +"/cmd/put";
HttpPost httppost = new HttpPost(url);
httppost.setHeader("content-type", "application/octet-stream");
httppost.setHeader("x-my-policy", policy );
httppost.setHeader("Connection", "keep-alive");
ByteArrayEntity entity = new ByteArrayEntity(data);
httppost.setEntity(entity);
String key = "";
try {
HttpResponse response = httpClient.execute(httppost, context);
HttpEntity entity2 = response.getEntity();
if (entity2 != null) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
Header[] allHeaders = response.getAllHeaders();
for (int i = 0; i < allHeaders.length; i++) {
Header header = allHeaders[i];
if ("x-my-key".equals(header.getName())) {
key = header.getValue();
//System.out.println(header.getName() +" : "+ header.getValue());
}
}
} else {
return "MyError:"+statusCode;
}
} else {
return "MyError: entity2 is null "+ entity2;
}
EntityUtils.consume(entity);
} catch (Exception e) {
httppost.abort();
logger.error("My出现异常: " + e.toString());
return "MyError:" + e.getMessage();
}finally{
httppost.releaseConnection();
}