英文:
How to set an executor for async-http-client?
问题
如何设置async-http-client的线程池大小?我找到了这个链接 - https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html 用于创建线程池。但是我无法为客户端设置执行器。我的连接是有限的,因此我想将线程池中的线程数量限制为只有10个。我正在使用async-http-client:2.12.3
final AsyncHttpClient http =
asyncHttpClient(
config().setThreadPoolName("CustomThreadPool"));
英文:
How to setup the thread pool size for async-http-client? I found this - https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html to create a thread pool. But I am not able to set the executor for the client. My connections are limited and hence I want to restrict the number of threads to only 10 in the thread pool. I am using async-http-client:2.12.3
final AsyncHttpClient http =
asyncHttpClient(
config().setThreadPoolName("CustomThreadPool"));
答案1
得分: 1
你可以这样做:
public AsyncHttpClient asyncHttpClient() {
ExecutorService executorService = Executors.newFixedThreadPool(10); // 创建执行器
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(10, executorService); // 指定此实例要使用的线程数并提供您的执行器
DefaultAsyncHttpClientConfig.Builder configBuilder = Dsl.config()
.setEventLoopGroup(eventLoopGroup);
return Dsl.asyncHttpClient(configBuilder.build());
}
英文:
You can do it like this:
public AsyncHttpClient asyncHttpClient() {
ExecutorService executorService = Executors.newFixedThreadPool(10); // create executor
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(10, executorService); // specify number of threads to be used by this instance and provide your executor
DefaultAsyncHttpClientConfig.Builder configBuilder = Dsl.config()
.setEventLoopGroup(eventLoopGroup);
return Dsl.asyncHttpClient(configBuilder.build());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论