2017-02-16 32 views
5

Birkaç saniye sürecek bazı API'ları çağırmak için Spring 4.2.3 AsyncRestTemplate.exchange() kullanıyorum ve bu dinlemeyi dinleyebilmeniz için listenableFuture.get (1, TimeUnit.SECONDS) 1 saniye ve sonra TimeOutException. İşte Dinleme zaman aşımıyla birlikte Dinlenebilme özelliği

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 

bir örnektir: yerine ne olur

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"); 

Çıktı listenableFuture.get() API çağrısı bütün zaman (1 saniyeden fazla) için engeller olmasıdır beklendiği gibi AsyncRestTemplate tarafından oluşturulmamış zaman ListenableFuture.get() çalışacağını

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

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

neden günlükte 5. satırdaki URL'de "wait? Seconds = 5" yazıyor? –

+0

@vstromcoder X'i sayı olarak alan ve X saniye bekledikten sonra Tamam'a geri döndüğünüz yerel API'yi oluşturdum. Yani exchange'e aktardığım URL bu API. Bu yüzden API bitmeden sadece 1 saniye beklemeye çalışıyorum (5 saniye sonra) – Yoni

cevap

1

Yo u kodu tamamen doğrudur. Bir soruna neden olan şey, ilkbahar çerçevesinde bir hatadır. Yine de, baharın sorun izleyicisinde (veya muhtemelen belgelenmemiş) onu bulmayı başaramamış olmama rağmen, bağımlılıkları güncelleyerek bunu düzeltebilirsiniz. Elbette, kodunuz spring-web> = 4.3.2.RELEASE sürümü ile çalışacaktır.

İlgili konular