如何增加AsyncRestTemplate类的超时?
我用spring框架和REST开发了一些异步Web服务,我从使用spring类AsyncRestTemplate
创建的客户端中使用它。 类返回一个对象ListenableFuture<ResponseEntity>
(使用方法getForEntity
),它带来Web服务返回的值(使用方法.get():
)。 它工作正常,但是当Web服务花费很多时间时,方法isDone()
的ListenableFuture
类返回值true
,即使Web服务尚未完成工作。
如果我尝试使用客户端中的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"
有人知道我该如何解决这个问题? 我希望客户端向我显示Web服务响应,即使Web服务需要花费很多时间(我想增加超时)。
服务器代码如下:
配置类:
@Configuration @EnableAsync public class ConfigurationClass { @Bean public Executor threadPoolTaskExecutor() { return new ThreadPoolTaskExecutor(); } }
控制器类:
@RestController @RequestMapping("/server") public class ControllerClass { @GetMapping("/start") @Async public CompletableFuture callService() throws InterruptedException{ Thread.sleep(100000L); return CompletableFuture.completedFuture("OK"); } }
客户端代码(使用者)如下:
@RestController @RequestMapping("/client") public class ControllerClass { private ListenableFuture<ResponseEntity> 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 getResult() { try { return entity.get(); } catch (Exception e) { throw new RuntimeException(e); } } }
我试图在application.property文件中增加属性超时,但它不起作用。
# SPRING MVC (WebMvcProperties) spring.mvc.async.request-timeout= 500000 # Amount of time before asynchronous request handling times out.
谢谢你的帮助,问候。