私はSpring 4.2.3 AsyncRestTemplate.exchange()を使用して、数秒かかるAPIを呼び出しています。listenableFuture.get(1、TimeUnit.SECONDS)はブロックされます1秒後にTimeOutExceptionをスローします。ここで タイムアウトでListenableFutureをブロックする
09:15:21.596 DEBUG [main] org.springframework.web.client.AsyncRestTemplate:78 - Created asynchronous GET request for "http://localhost:4567/oia/wait?seconds=5"
09:15:21.666 DEBUG [main] org.springframework.web.client.RestTemplate:720 - Setting request Accept header to [text/plain, application/xml, text/xml, application/json, application/*+xml, application/*+json, */*]
09:15:21.679 DEBUG [main] com.zazma.flow.utils.FutureTest:74 - before callback
09:15:21.679 DEBUG [main] com.zazma.flow.utils.FutureTest:95 - before blocking
09:15:26.709 DEBUG [main] org.springframework.web.client.AsyncRestTemplate:576 - Async GET request for "http://localhost:4567/oia/wait?seconds=5" resulted in 200 (OK)
09:15:26.711 DEBUG [main] org.springframework.web.client.RestTemplate:101 - Reading [java.lang.String] as "text/html;charset=utf-8" using [[email protected]44431a]
09:15:26.717 INFO [main] com.zazma.flow.utils.FutureTest:105 - FINISHED
は例です:代わりに何が起こる
は
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
ListenableFuture<ResponseEntity<String>> listenableFuture = restTemplate.exchange(URL, HttpMethod.GET, null, String.class);
log.debug("before callback");
//...add callbacks
try{
log.debug("before blocking");
listenableFuture.get(1, TimeUnit.SECONDS);
}catch (InterruptedException e) {
log.error(":GOT InterruptedException");
} catch (ExecutionException e) {
log.error(":GOT ExecutionException");
} catch (TimeoutException e) {
log.info(":GOT TimeoutException");
}
log.info("FINISHED");
が出力listenableFuture.getは()API呼び出しの全体の時間(1秒以上)のためにブロックすることですListenableFuture.get()がAsyncRestTemplateで作成されていない場合に期待通りに機能することを確認してください。
SimpleAsyncTaskExecutor te = new SimpleAsyncTaskExecutor();
ListenableFuture<String> lf = te.submitListenable(() -> {
Thread.sleep(8000);
return "OK";
});
lf.get(1, TimeUnit.SECONDS);
ログの5行目のURLに「wait?seconds = 5」と表示されるのはなぜですか? –
@vstromcoder私は数としてXを取得し、X秒待ってからOKを返すローカルAPIを作成しました。だから私は交換に渡しているURLはそのAPIです。私はAPIが(5秒後に)終了する前に1秒間待つことを試みているのです – Yoni