javaasynchttpclient

How to set an executor for async-http-client?


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"));

Solution

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