在RequestConfig(Apache HTTP异步客户端4.1.2)中无法将套接字超时设置为小于1000毫秒

Rah*_*lya 6 java apache-httpclient-4.x apache-httpasyncclient

以下是我的代码

       RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(100)
                .setConnectTimeout(100)
                .setConnectionRequestTimeout(100).build();


        CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .build();

        httpClient.start();
Run Code Online (Sandbox Code Playgroud)

根据setSocketTimeout值,它应该在100毫秒内超时,但超时需要1000毫秒.但是,setSocketTimeout表示所有大于1000毫秒的值.

ok2*_*k2c 9

这种行为是故意的.I/O选择器线程需要定期迭代现有的i/o会话,并在I/O不活动时触发套接字超时事件.此操作可能变得非常昂贵,尤其是随着并发会话数量的增加.默认情况下,i/o选择间隔设置为1000 ms,因此默认情况下套接字超时的粒度为1秒.可以减少选择间隔并使I/O选择器线程更频繁地迭代会话,代价是CPU利用率更高.使用1ms的选择间隔,I/O选择器线程将在繁忙的循环中有效运行.

IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
        .setSelectInterval(100)
        .setSoTimeout(100)
        .setConnectTimeout(100)
        .build();
CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
        .setDefaultIOReactorConfig(ioReactorConfig)
        .build();
Run Code Online (Sandbox Code Playgroud)