作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 Apache http 客户端的新手,正在尝试从网站获取状态代码。在 Apache http 教程中找到以下示例。
import java.util.concurrent.CountDownLatch;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
public class Abc {
static long d2;
public static void main(final String[] args) throws Exception {
d2=System.currentTimeMillis();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
try {
httpclient.start();
final HttpGet[] requests = new HttpGet[] {
new HttpGet("http://192.168.26.175:8080/examples/eye/abc10000.jsp")
};
final CountDownLatch latch = new CountDownLatch(1);
for (int v=0;v<1000;v++) {
httpclient.execute(requests[0], new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(requests[0].getRequestLine() + "->" + response.getStatusLine());
}
public void failed(final Exception ex) {
latch.countDown();
System.out.println(requests[0].getRequestLine() + "->" + ex);
}
public void cancelled() {
latch.countDown();
System.out.println(requests[0].getRequestLine() + " cancelled");
}
});
}
latch.await();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
long d1=System.currentTimeMillis();
System.out.println(d1-d2);
}
}
是真的异步还是串行调用。必须做些什么才能使调用异步且更快。
最佳答案
由于请求通过相同的路线进行,因此必须进行以下更改。
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.setMaxConnPerRoute(1000)
.setMaxConnTotal(1000)
.build();
关于java - Apache async HttpClient 速度不快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21727604/
我可以使用 IntelliJ 浏览可用的 javadoc 吗? 例如,我在 Maven 中配置了一些库,它的 javadoc 仅在 Maven 存储库上可用。 懒得手动下载,解压后用浏览器打开。 是否
我是一名优秀的程序员,十分优秀!