java SimpleClientHttpRequestFactory 与 HttpComponentsClientHttpRequestFactory 的 Http 请求超时与 RestTemplate?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25698072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:28:25  来源:igfitidea点击:

SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?

javamultithreadingspringhttprequestresttemplate

提问by AKIWEB

I am working on a project in which I need to make a HTTP URL call to my server which is running Restful Service which returns back the response as a JSON String.

我正在开发一个项目,在该项目中我需要对运行 Restful 服务的服务器进行 HTTP URL 调用,该服务将响应作为JSON String.

Below is my main code which is using the future and callables:

下面是我使用 future 和 callables 的主要代码:

public class TimeoutThreadExample {

    private ExecutorService executor = Executors.newFixedThreadPool(10);
    private RestTemplate restTemplate = new RestTemplate();

    public String getData() {
        Future<String> future = executor.submit(new Task(restTemplate));
        String response = null;

        try {
            response = future.get(500, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return response;
    }
}

Below is my Taskclass which implements the Callableinterface and uses the RestTemplate:

下面是我的Task类,它实现了Callable接口并使用了RestTemplate

class Task implements Callable<String> {

    private RestTemplate restTemplate;

    public Task(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String call() throws Exception {

        String url = "some_url";
        String response = restTemplate.getForObject(url, String.class);

        return response;
    }
}

Problem Statement:

问题陈述:

As you can see above, I am using default way of executing the URL using RestTemplatewhich doesn't use any Http Request timeout so that means internally it is using -1as the readand connectiontimeout.

正如你可以在上面看到,我使用的使用执行URL的默认方式RestTemplate,这样的手段在内部它使用不使用任何HTTP请求超时-1作为readconnection超时。

Now what I am looking to do is, I want to set up Http Request timeout using RestTemplatein my above code efficiently. And I am not sure which class I need to use for that, I can see HttpComponentsClientHttpRequestFactoryand SimpleClientHttpRequestFactoryso not sure which one I need to use?

现在我要做的是,我想RestTemplate在上面的代码中有效地设置 Http 请求超时。我不确定我需要使用哪个类,我可以看到HttpComponentsClientHttpRequestFactorySimpleClientHttpRequestFactory所以不确定我需要使用哪个类?

Any simple example basis on my above code will help me understand better on how to set the Http Request timeout using RestTemplate.

基于上述代码的任何简单示例都将帮助我更好地理解如何使用RestTemplate.

And also does my Http Request timeout value should be less than future timeout value?

而且我的 Http 请求超时值是否应该小于未来的超时值?

  • HttpComponentsClientHttpRequestFactoryvs SimpleClientHttpRequestFactory. Which one to use?
  • Does my Http Request timeout value should be less than future timeout value?
  • HttpComponentsClientHttpRequestFactorySimpleClientHttpRequestFactory. 使用哪一种?
  • 我的 Http 请求超时值是否应该小于未来的超时值?

回答by ?zbek

By default RestTemplate uses SimpleClientHttpRequestFactorywhich depends on default configuration of HttpURLConnection.

默认情况下,RestTemplate 使用SimpleClientHttpRequestFactory取决于HttpURLConnection.

You can configure them by using below attributes:

您可以使用以下属性配置它们:

-Dsun.net.client.defaultConnectTimeout=TimeoutInMiliSec 
-Dsun.net.client.defaultReadTimeout=TimeoutInMiliSec 

If you want to use HttpComponentsClientHttpRequestFactory- it has a connection pooling configuration which SimpleClientHttpRequestFactorydoes not have.

如果你想使用HttpComponentsClientHttpRequestFactory- 它有一个没有的连接池配置SimpleClientHttpRequestFactory

A sample code for using HttpComponentsClientHttpRequestFactory:

使用的示例代码HttpComponentsClientHttpRequestFactory

public class TimeoutThreadExample {

    private ExecutorService executor = Executors.newFixedThreadPool(10);
    private static final RestTemplate restTemplate = createRestTemplate();

    private static RestTemplate createRestTemplate(){
       HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
       requestFactory.setReadTimeout(READ_TIME_OUT);
       requestFactory.setConnectTimeout(CONNECTION_TIME_OUT);
       return new RestTemplate(requestFactory);
     }

    public String getData() {
        Future<String> future = executor.submit(new Task(restTemplate));
        String response = null;

        try {
            response = future.get(500, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return response;
    }
}