2016-09-21 45 views
-1

我想从REST客户端调用多个REST服务,我怎样才能将它们称为每个调用使用的单线程。我婉称他们为并行如何一次调用多个REST服务?

+0

'''java.util.concurrent.Future'''是你的朋友 –

+0

请阅读我们的[允许哪些主题](http://stackoverflow.com/help/on-topic)和[如何提问好问题](http://stackoverflow.com/help/how-to-ask)指南。正如目前所写,它是SO的Off主题。 – dubes

+0

感谢Pawel为此提供了深入的见解 – Nallarc

回答

1

下面是多个数据库请求的代码示例,我为我的目的而作出

CompletableFuture<Company> companyCompletableFuture = CompletableFuture.supplyAsync(() -> { 
      return Company.find.where().eq("id", id).findUnique(); 
     }); 

     CompletableFuture<List<Domain>> domainsCompletableFuture = CompletableFuture.supplyAsync(() -> { 
      return Domain.find.where().eq("company_id", id).findList(); 
     }); 

     // wait for all the data 
     CompletableFuture allDoneFuture = CompletableFuture.allOf(companyCompletableFuture, domainsCompletableFuture); 

allDoneFuture.get(); // wait for all done 
company = companyCompletableFuture.get(); 
domain = domainsCompletableFuture.get() 

你需要改变它做的http请求,使能对你的porpose 。

+0

感谢Md提供代码的详细信息,这真的很有帮助 – Nallarc