我在代码中使用异步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上看到了指令我没有找到线程池的信息
UPDATE 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的附加线程仍然被创建而没有被破坏。
发布于 2019-08-08 20:33:53
AHC有两种类型的线程:
资料来源:GitHub杂志:https://github.com/AsyncHttpClient/async-http-client/issues/1658
https://stackoverflow.com/questions/56343520
复制相似问题