[英]SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?
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 Service 的服务器进行 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.正如您在上面看到的,我使用RestTemplate
执行 URL 的默认方式,它不使用任何 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.现在我要做的是,我想在上面的代码中使用RestTemplate
有效地设置 Http 请求超时。 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?而且我不确定我需要使用哪个类,我可以看到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
设置 Http 请求超时。
And also does my Http Request timeout value should be less than future timeout value?而且我的 Http 请求超时值是否应该小于未来的超时值?
HttpComponentsClientHttpRequestFactory
vs SimpleClientHttpRequestFactory
. HttpComponentsClientHttpRequestFactory
与SimpleClientHttpRequestFactory
。 Which one to use?使用哪一种?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;
}
}
SimpleClientHttpRequestFactory uses the standard JDK's HTTP library, and hence does not support methods like HttpMethod.PATCH
. SimpleClientHttpRequestFactory 使用标准 JDK 的 HTTP 库,因此不支持HttpMethod.PATCH
方法。 So it's better to use HttpComponentsClientHttpRequestFactory
now than change it later when you have to.因此,现在最好使用HttpComponentsClientHttpRequestFactory
不是以后需要时更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.