如何使用AsyncRestTemplate同时进行多个调用? [英] How to use AsyncRestTemplate to make multiple calls simultaneously?

查看:1618
本文介绍了如何使用AsyncRestTemplate同时进行多个调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何有效地使用 AsyncRestTemplate 进行外部服务调用。对于以下代码:

I don't understand how to use AsyncRestTemplate effectively for making external service calls. For the code below:

class Foo {

    public void doStuff() {
        Future<ResponseEntity<String>> future1 = asyncRestTemplate.getForEntity(
                url1, String.class);
        String response1 = future1.get();

        Future<ResponseEntity<String>> future2 = asyncRestTemplate.getForEntity(
                url2, String.class);
        String response2 = future2.get();

        Future<ResponseEntity<String>> future3 = asyncRestTemplate.getForEntity(
                url3, String.class);
        String response3 = future3.get();
    }
}

理想情况下我想同时执行所有3个调用并处理结果一旦完成。 然而 每次外部服务调用,直到 get()被调用但 get()被阻止。那么这不是打败了 AsyncRestTemplate 的目的吗?我不妨使用 RestTemplate

Ideally I want to execute all 3 calls simultaneously and process the results once they're all done. However each external service call is not fetched until get() is called but get() is blocked. So doesn't that defeat the purpose of AsyncRestTemplate? I might as well use RestTemplate.

所以我不明白我怎么能让它们同时执行?

So I don't understaand how I can get them to execute simultaneously?

推荐答案

在发送所有内容之前,不要调用阻止 get()您的异步调用:

Simply don't call blocking get() before dispatching all of your asynchronous calls:

class Foo {
  public void doStuff() {
    ListenableFuture<ResponseEntity<String>> future1 = asyncRestTemplate
        .getForEntity(url1, String.class);
    ListenableFuture<ResponseEntity<String>> future2 = asyncRestTemplate
        .getForEntity(url2, String.class);
    ListenableFuture<ResponseEntity<String>> future3 = asyncRestTemplate
        .getForEntity(url3, String.class);

    String response1 = future1.get();
    String response2 = future2.get();
    String response3 = future3.get();
  }
}

您可以同时执行发送获取在循环中,但请注意,当前的结果收集效率低下,因为它会陷入下一个未完成的未来。

You can do both dispatch and get in loops, but note that current results gathering is inefficient as it would get stuck on the next unfinished future.

您可以将所有期货添加到集合中,并通过它迭代测试每个未来非阻塞 isDone()。当该调用返回true时,您可以调用 get()

You could add all the futures to a collection, and iterate through it testing each future for non blocking isDone(). When that call returns true, you can then call get().

这样,您的整体结果收集将被优化,而不是按照调用 get()的顺序等待下一个缓慢的未来结果 s。

This way your en masse results gathering will be optimised rather than waiting on the next slow future result in the order of calling get()s.

更好的是你可以在<返回的每个 ListenableFuture 中注册回调(运行时) code> AccyncRestTemplate ,您不必担心周期性地检查潜在结果。

Better still you can register callbacks (runtimes) within each ListenableFuture returned by AccyncRestTemplate and you don't have to worry about cyclically inspecting the potential results.

这篇关于如何使用AsyncRestTemplate同时进行多个调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆