我正在创建一个测试应用程序,通过创建一堆出站https连接来加载测试彗星servlet(每个循环都在自己的线程中)。
并发出站http请求的数量是否有限制?如果是这样,它是OS级问题(xp 32位)还是Java?运行大约100个连接时出现以下异常。错误不是立即发生,而是在50-150循环(有时更多)之后,导致我怀疑它实际上可能是我没有正确释放资源:
java.net.BindException: Address already in use: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at fqexconnectiontesting.FQEXHttpClient.run(FQEXHttpClient.java:158)
at java.lang.Thread.run(Thread.java:662)
我的代码:
while(true){
try{
connection =(HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("keep-alive", "true");
connection.setRequestMethod("GET");
input = new DataInputStream( connection.getInputStream() );
b = new byte[4096];
totalBytes += input.read(b);
input.close();
b = null;
connection.disconnect();
connection = null;
Thread.sleep(100);
}
任何想法都赞赏。
谢谢, Dazz
答案 0 :(得分:2)
默认情况下,Java最多只能打开5个到同一主机的持久连接。 但是你的代码使用了disconnect()方法,AFAIK阻止了持久连接。
您最好阅读有关Persistent Connections的文档。将示例代码放在最后,以获得完全有效的持久连接。
可能会解决您的问题。