官方文档地址:Timeouts


    配置请求超时可以通过提供一个RequestConfigCallback实例来完成,同时通过其构建器构建RestClient。该接口有一个方法,该方法接收org.apache.http.client.config.RequestConfig.Builder的实例作为参数,并具有相同的返回类型。可以修改并返回请求配置生成器。在下面的例子中,我们增加了连接超时(默认为 1 秒)和 socket 超时(默认为 30 秒)。

    1. RestClientBuilder builder = RestClient.builder(
    2. new HttpHost("localhost", 9200))
    3. .setRequestConfigCallback(
    4. new RestClientBuilder.RequestConfigCallback() {
    5. @Override
    6. public RequestConfig.Builder customizeRequestConfig(
    7. RequestConfig.Builder requestConfigBuilder) {
    8. return requestConfigBuilder
    9. .setConnectTimeout(5000)
    10. .setSocketTimeout(60000);
    11. }
    12. });

    超时也可以通过RequestOptions设置,它覆盖了 RestClient``customizeRequestConfig

    1. RequestConfig requestConfig = RequestConfig.custom()
    2. .setConnectTimeout(5000)
    3. .setSocketTimeout(60000)
    4. .build();
    5. RequestOptions options = RequestOptions.DEFAULT.toBuilder()
    6. .setRequestConfig(requestConfig)
    7. .build();