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
SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?
提问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 Task
class which implements the Callable
interface 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 RestTemplate
which doesn't use any Http Request timeout so that means internally it is using -1
as the read
and connection
timeout.
正如你可以在上面看到,我使用的使用执行URL的默认方式RestTemplate
,这样的手段在内部它使用不使用任何HTTP请求超时-1
作为read
和connection
超时。
Now what I am looking to do is, I want to set up Http Request timeout using RestTemplate
in my above code efficiently. And I am not sure which class I need to use for that, I can see HttpComponentsClientHttpRequestFactory
and SimpleClientHttpRequestFactory
so not sure which one I need to use?
现在我要做的是,我想RestTemplate
在上面的代码中有效地设置 Http 请求超时。我不确定我需要使用哪个类,我可以看到HttpComponentsClientHttpRequestFactory
,SimpleClientHttpRequestFactory
所以不确定我需要使用哪个类?
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 请求超时值是否应该小于未来的超时值?
HttpComponentsClientHttpRequestFactory
vsSimpleClientHttpRequestFactory
. Which one to use?- Does my Http Request timeout value should be less than future timeout value?
HttpComponentsClientHttpRequestFactory
与SimpleClientHttpRequestFactory
. 使用哪一种?- 我的 Http 请求超时值是否应该小于未来的超时值?
回答by ?zbek
By default RestTemplate uses SimpleClientHttpRequestFactory
which 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 SimpleClientHttpRequestFactory
does 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;
}
}