[英]How to increase timeout AsyncRestTemplate class?
I have developed some async web services with spring framework and REST, I have consumed it from a client created with spring class AsyncRestTemplate
. 我已经使用Spring框架和REST开发了一些异步Web服务,我从使用Spring类
AsyncRestTemplate
创建的客户端中使用了它。 Class return an object ListenableFuture<ResponseEntity<T>>
(with the method getForEntity
), which brings the value returned by the web service (with the method .get():<T>
) . 类返回一个对象
ListenableFuture<ResponseEntity<T>>
(使用getForEntity
方法),该对象将带来Web服务返回的值(使用.get():<T>
)。 It works fine, however when the web service takes a lot time the method isDone()
of ListenableFuture
class return a value true
, even when the web service has not finished to work. 它工作正常,但是当Web服务花费大量时间时,即使Web服务尚未完成工作,
ListenableFuture
类的isDone()
方法也将返回值true
。
If I try to recover the web service response with the method get()
in the client and It has late a lot of time, I always get the follows message: 如果我尝试使用客户端中的
get()
方法恢复Web服务响应,并且时间已经很晚了,那么我总是会收到以下消息:
"timestamp": "2018-05-29T22:42:26.978+0000",
"status": 500,
"error": "Internal Server Error",
"message": "java.util.concurrent.ExecutionException: org.springframework.web.client.HttpServerErrorException: 503 null",
"path": "/client/result"
Does someone knows how can i solve the problem?. 有人知道我该如何解决问题? I want the client shows me the web service response, even when the web service takes a lot time (I want to increase the timeout).
我希望客户端向我显示Web服务响应,即使Web服务花费很多时间(我想增加超时时间)也是如此。
The server codes are the following: 服务器代码如下:
Configuration Class: 配置类别:
@Configuration
@EnableAsync
public class ConfigurationClass {
@Bean
public Executor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
}
Controller class: 控制器类:
@RestController
@RequestMapping("/server")
public class ControllerClass {
@GetMapping("/start")
@Async
public CompletableFuture<String> callService() throws InterruptedException{
Thread.sleep(100000L);
return CompletableFuture.completedFuture("OK");
}
}
The client code (consumer) is the following: 客户端代码(消费者)如下:
@RestController
@RequestMapping("/client")
public class ControllerClass {
private ListenableFuture<ResponseEntity<String>> entity;
@GetMapping("/start")
@Async
public void callService() throws InterruptedException {
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
entity = restTemplate.getForEntity("http://localhost:8080/server/start",
String.class);
}
@GetMapping("/state")
public boolean getState() {
try {
return entity.isDone();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@GetMapping("/result")
public ResponseEntity<String> getResult() {
try {
return entity.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
I tried to increase the property timeout in application.property file, but it did not work. 我试图增加application.property文件中的属性超时,但是没有用。
# SPRING MVC (WebMvcProperties)
spring.mvc.async.request-timeout= 500000 # Amount of time before asynchronous request handling times out.
Thanks for your help, Regards. 谢谢您的帮助,问候。
For a better maintenance, you can config a AsyncRestTemplate bean: 为了更好的维护,您可以配置AsyncRestTemplate bean:
@Bean
public AsyncRestTemplate asyncRestTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setTaskExecutor(new SimpleAsyncTaskExecutor());
factory.setConnectTimeout(1000);//milliseconds
factory.setReadTimeout(2000);//milliseconds
return new AsyncRestTemplate(factory);
}
And then, autowired this bean: 然后,自动装配该bean:
@Autowired
private AsyncRestTemplate restTemplate;
After that, update your callService: 之后,更新您的callService:
@GetMapping("/start")
public void callService() throws InterruptedException {
entity = restTemplate.getForEntity("http://localhost:8080/server/start",
String.class);
}
You can remove the @Async
Annotation as the AsyncRestTemplate
is Asynchronous. 您可以删除
@Async
注释,因为AsyncRestTemplate
是异步的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.