java - 使用 Async HTTP Client netty 客户端会在高负载下爆炸?

标签 java netty asynchttpclient

我在使用 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 客户端吗?

最佳答案

您的问题是您在开始读取文件之前没有设置任何缓冲区大小。看看这个例子

private static final int EXT_DEFAULT_BUFFER_SIZE = 1024 * 8;
InputStream   inputStream=this.getClass().getClassLoader().getResourceAsStream("Resource_Name");
public static String copyLargeExt(InputStream input) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[EXT_DEFAULT_BUFFER_SIZE];
        int n = 0;
        while(-1 != (n = input.read(buffer))) {
            baos.write(buffer, 0, n);
        }
        return baos.toString();
    }

关于java - 使用 Async HTTP Client netty 客户端会在高负载下爆炸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280043/

相关文章:

java - AsyncHttpClient 中的 ArrayList iterator.remove() 抛出 IllegalStateException

java - 使用 Fabric8 关闭 Kubernetes 客户端

java - 为什么这个 Guava 按值排序 map 有效?

java - 网络 4.1 : Send multiple WebSocketFrame Fragments and Closer with ContinuationWebSocketFrame

java - Netty读取确切的字节数

java - 从源代码构建 netty 时出现问题

Android:无法取消异步任务

java - 远程调试作为 Windows 服务启动的 Java 应用程序

java - 通过多个并行线程 NIO 异步写入(追加)到文件

java - 如何在 Java 中使用 Google S2 库创建多边形