[英]How to integrate Spring Retry with AsyncRestTemplate
如何使用AsyncRestTemplate
將Spring Retry
與外部調用集成? 如果不可能,是否有另一個支持它的框架?
我的用例:
public void doSomething() throws ExecutionException, InterruptedException {
ListenableFuture<ResponseEntity<String>> future = asyncRestTemplate.getForEntity("http://localhost/foo", String.class);
// do some tasks here
ResponseEntity<String> stringResponseEntity = future.get(); // <-- How do you retry just this call?
}
你如何重試這個future.get()
調用? 如果外部服務返回404,我想避免再次調用這些任務,只是重試外部調用? 我不能只是包裝future.get()
與retryTemplate.execute()
因為它實際上並不會再拍調用外部服務。
您必須將整個doSomething
(或至少模板操作和get)包裝在重試模板中。
編輯
您可以在未來添加ListenableFutureCallback
,而不是調用get()
; 像這樣的東西......
final AtomicReference<ListenableFuture<ResponseEntity<String>>> future =
new AtomicReference<>(asyncRestTemplate.getForEntity("http://localhost/foo", String.class));
final CountDownLatch latch = new CountDownLatch(1);
future.addCallback(new ListenableFutureCallback<String>() {
int retries;
@Override
public void onSuccess(String result) {
if (notTheResultIWant) {
future.set(asyncTemplate.getFor (...));
future.get().addCallback(this);
retries++;
}
else {
latch.countDown();
}
}
@Override
public void onFailure(Throwable ex) {
latch.countDown();
}
});
if (latch.await(10, Timeunit.SECONDS) {
...
future.get().get();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.