如何为async-http-client设置执行器?

huangapple go评论119阅读模式
英文:

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());
}

huangapple
  • 本文由 发表于 2023年6月13日 05:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定