AsyncHttpClient 创建了多少线程?

AsyncHttpClient creates how much threads?

我在我的代码中使用异步 http 客户端来异步处理 GET 响应 我可以同时 运行 100 个请求。

我只在容器中的 httpClient 实例上使用

@Bean(destroyMethod = "close")
open fun httpClient() = Dsl.asyncHttpClient()

代码看起来像

fun method(): CompletableFuture<String> {
    return httpClient.prepareGet("someUrl").execute()
        .toCompletableFuture()
        .thenApply(::getResponseBody)
}

功能正常。在我的测试中,我使用具有相同 url 地址的模拟端点。但我的期望是所有请求都在多个线程中处理,但在分析器中我可以看到为 AsyncHttpClient 创建了 16 个线程,并且它们没有被销毁,即使没有请求发送。

我的期望是

我是不是错过了一些我的期望?

更新 1 我在 https://github.com/AsyncHttpClient/async-http-client/wiki/Connection-pooling 上看到了说明 我没有找到关于线程池的信息

更新 2 我还创建了方法来做同样的事情,但是有处理程序和额外的执行程序池

实用方法看起来像

fun <Value, Result> CompletableFuture<Value>.handleResultAsync(executor: Executor, initResultHandler: ResultHandler<Value, Result>.() -> Unit): CompletableFuture<Result> {
    val rh = ResultHandler<Value, Result>()
    rh.initResultHandler()

    val handler = BiFunction { value: Value?, exception: Throwable? ->
        if (exception == null) rh.success?.invoke(value) else rh.fail?.invoke(exception)
    }

    return handleAsync(handler, executor)
}

更新后的方法看起来像

fun method(): CompletableFuture<String> {
    return httpClient.prepareGet("someUrl").execute()
        .toCompletableFuture()
        .handleResultAsync(executor) {
            success = {response ->
                logger.info("ok")
                getResponseBody(response!!)
            }
            fail = { ex ->
                logger.error("Failed to execute request", ex)
                throw ex
            }
    }
}

然后我可以看到 GET 方法的结果是在线程池提供的线程中执行的(之前的结果是在 "AsyncHttpClient-3-x" 中执行的),但是 AsyncHttpClient 的附加线程仍然被创建并且没有被销毁。

AHC 有两种类型的线程:

  1. 为I/O操作。 在你的屏幕上,它是 AsyncHttpClient-x-x 线程。 AHC 创建了其中的 2*core_number
  2. 超时。 在您的屏幕上,它是 AsyncHttpClient-timer-1-1 线程。应该 只有 一个.

来源:GitHub 上的问题:https://github.com/AsyncHttpClient/async-http-client/issues/1658