2017-02-16 1 views
5

몇 초가 걸릴 API를 호출하기 위해 Spring 4.2.3 AsyncRestTemplate.exchange()를 사용하고 있으며 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 초 이상)에 대한 차단한다는 것이다 AsyncRestTemplate에 의해 생성되지 않으면 ListenableFuture.get()이 예상대로 작동합니다.

SimpleAsyncTaskExecutor te = new SimpleAsyncTaskExecutor(); 
    ListenableFuture<String> lf = te.submitListenable(() -> { 
     Thread.sleep(8000); 
     return "OK"; 
    }); 

    lf.get(1, TimeUnit.SECONDS); 
+0

로그의 5 행에있는 URL에 "wait? seconds = 5"가 표시되는 이유는 무엇입니까? –

+0

@vstromcoder 저는 X를 숫자로 얻고 X 초를 기다린 다음 OK를 반환하는 로컬 API를 만들었습니다. 따라서 교환기로 전달하는 URL은 API입니다. API가 끝나기 전에 1 초만 기다려야하는 이유 (5 초 후) – Yoni

답변

1

Yo u 코드가 완벽하게 맞습니다. 문제의 원인은 스프링 프레임 워크의 버그입니다. , 나는 봄의 문제점 추적자에서 그것을 발견하는 것을 처리하지 않았더라도 (또는 가능하게 문서화되지 않는다), 당신은 의존성을 새롭게해서 그것을 고칠 수있다. 확실히, 코드는 spring-web> = 4.3.2.RELEASE 버전에서 작동합니다.

관련 문제