【发布时间】:2023-03-22 01:20:02
【问题描述】:
我在使用 AsyncHTTPClient 测试的一些代码时遇到问题。这是一般配置
this.config = (new AsyncHttpClientConfig.Builder())
.setAllowPoolingConnection(true)
.setAllowSslConnectionPool(true)
.addRequestFilter(new ThrottleRequestFilter(10))
.setMaximumConnectionsPerHost(20)
//.setMaximumConnectionsTotal(20)
.setRequestTimeoutInMs(100000)
.build();
this.client = new AsyncHttpClient(new NettyAsyncHttpProvider(config));
(请注意,由于在使用 ThrottleRequestFilter 时出现了一些奇怪的错误,最大连接数被注释掉了,请参阅此处https://groups.google.com/forum/?fromgroups=#!topic/asynchttpclient/nEnQnPtCP2g)
下面是一些测试代码,
FileInputStream fstream = new FileInputStream("import.txt");
DataInputStream dstream = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
while ((line = reader.readLine()) != null) {
//Some code to build and create a request using the build function and then perform a GET operation. Request is built using Java's Future object from java.concurrent
}
当import.txt文件小于100行这样的简单文本时
196 242 3 881250949
一切正常,所有请求都通过,当您检查响应时一切正常。任何超过 100 的东西,我都会开始超时,如果超过 1000,我实际上会开始遇到 permGen 内存错误。
我认为 ThrottleRequestFilter 应该将最大线程数限制为 10,并且一次只能处理 10 个。为什么文本文件超过 100+ 行时会爆炸?
我也尝试过切换到使用 Grizzly 实现,但这也同样失败了。我开始怀疑这要么是我编写测试代码的方式,要么是 Async HTTP Client 在创建大量请求时实际上存在一些问题。如果是这样,还有其他好的 java 异步 http 客户端吗?
【问题讨论】:
标签: java netty asynchttpclient