线程挂起时使用java.net.HttpURLConnection';这是一个自https连接
我目前正在编写一个API,它需要从服务器A到自身建立HTTPS连接。使用Jmeter进行测试,我发现线程很容易挂起,甚至10个线程同时启动。但是,如果连接到其他服务器,则问题不存在。我只用了几行代码来测试它。它提出了同样的问题线程挂起时使用java.net.HttpURLConnection';这是一个自https连接,java,httpurlconnection,java-io,Java,Httpurlconnection,Java Io,我目前正在编写一个API,它需要从服务器A到自身建立HTTPS连接。使用Jmeter进行测试,我发现线程很容易挂起,甚至10个线程同时启动。但是,如果连接到其他服务器,则问题不存在。我只用了几行代码来测试它。它提出了同样的问题 try { URL url = new URL("some URL that connects to the server itself"); HttpURLConnection con = (HttpURLConnection) url.
try {
URL url = new URL("some URL that connects to the server itself");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(20000);
con.setReadTimeout(30000);
con.setRequestMethod("GET");
con.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = reader.readLine();
int count = 0;
while (line != null) {
count += line.length();
line = reader.readLine();
}
//con.disconnect();
}catch(Exception e){
e.printStackTrace();
}
在10个线程中,只有2个线程返回了预期的结果,其他线程似乎都因堆栈跟踪而超时
[err] java.net.SocketTimeoutException: Read timed out
[err] at java.net.SocketInputStream.socketRead0(Native Method)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:150)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:121)
[err] at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
[err] at sun.security.ssl.InputRecord.read(InputRecord.java:503)
[err] at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
[err] at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
[err] at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
[err] at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
[err] at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:224)
我使用-Djavax.net.debug=all启用调试选项,下面是控制台上显示的一些跟踪
Default Executor-thread-15, called closeInbound()
Default Executor-thread-15, fatal error: 80: Inbound closed before receiving peer's close_notify: possible truncation attack?
javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
%% Invalidated: [Session-19, TLS_RSA_WITH_AES_128_CBC_SHA256]
Default Executor-thread-15
, SEND TLSv1.2 ALERT:
fatal,
description = internal_error
Default Executor-thread-15, Exception sending alert: java.io.IOException: writer side was already closed.
我的问题是,为什么连接到服务器本身时会出现问题?为什么每次只有两个连接返回?我该如何解决它?
我尝试使用将http.maxConnections
设置为系统属性
System.setProperty(“http.maxConnections”,String.valueOf(300))代码>但没有帮助。可能会显示服务器端代码。还有为什么断开连接被注释掉了?这是我用来测试问题的大部分服务器端代码。我对此进行了注释,因为一旦我将Keep-Alive设置为与con.setRequestProperty的连接(“Keep-Alive”,“300”);con.setRequestProperty(“连接”、“保持活动”);如果我在连接上调用disconnect(),保持活动状态将不会生效,对吗?不管怎么说,我打电话给disconnect也没用。