dej*_*now 5 java apache-httpcomponents
我正在使用Apache HttpComponents 4.2.1,并且无法使HttpGet.abort()和HttpPost.abort()总是立即中止。它在大多数时间都有效,但是有时连接会阻塞直到超时。我注意到只有在我明确设置超时值时才会发生这种情况。
这是我的测试代码:
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
testAbort();
}
}
public static void testAbort() throws Exception {
String urlString = "http://slow.website.com";
final HttpGet httpGet = new HttpGet(urlString);
Runnable runnable = new Runnable() {
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000); // issue doesn't occur if I comment this line
httpClient.execute(httpGet);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
};
Thread t = new Thread(runnable);
t.start();
Thread.sleep(100);
httpGet.abort();
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出示例:
Request already aborted
Request already aborted
Request already aborted
Socket closed
Request already aborted
Connection has been shut down
Socket closed
Socket closed
Connect to slow.website.com:80 timed out
Connect to www.website.com:80 timed out
Run Code Online (Sandbox Code Playgroud)
您可能需要增加迭代次数或使用Thread.sleep()值来弄乱问题的发生。
根据HttpComponent的文档,“当HTTP请求中止时,可以通过抛出InterruptedIOException来保证其在I / O操作中被阻塞的执行线程解除阻塞”(链接)
我正在运行Mac OS X 10.8(Java版本1.6.0_29)。
任何想法可能是什么问题?