2011-11-30 66 views
3

我正在創建一個測試應用程序,通過創建一組出站https連接(每個循環都在它自己的線程中)來加載測試彗星servlet。Java HttpsURLConnection出站限制?

併發出站http請求的數量是否有限制?如果是這樣,它是一個操作系統級別的問題(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

回答

2

默認情況下打開的Java最多隻能到同一個主機5個持久連接。 但是你的代碼使用了AFAIK阻止持久連接的disconnect()方法。

您應該更好地閱讀有關Persistent Connections的文檔。接近結尾的示例代碼以獲得完全正常的持久連接。

可能會解決您的問題。

+0

謝謝,斷開連接似乎是必需的。解決的辦法是將http.maxConnections設置爲更高的數字。 – user481508