播放异步操作中的并行独立休息呼叫

时间:2019-05-21 02:36:05

标签: java playframework

我想在Java播放Async动作中进行3个独立的并行rest调用,并返回CompletionStage,以呈现带有剩余调用所有结果的视图。

我可以在Scala中通过理解来实现

def index() = Action.async { implicit request: Request[AnyContent] =>
    val f1 = getRecommendations()
    val f2 = getViewedItems()
    cal f2 = getCustomer()
    val futureResults = for {
      recommendations <- f1
      viewedItems <- f2
      customer <- f3
    } yield (recommendations, viewedItems, customer)
    futureResults.map(results => Ok(views.html.readingList.render(results._1, results._2, result_3))
    )
  }

我想在Java Play Async动作中做类似的事情

1 个答案:

答案 0 :(得分:0)

最后有一些时间可以解决这个问题。我在Java中获得的最接近的是:

    public CompletionStage<Result> index() {
    CompletableFuture<List<Item>> recommendationsCF = getRecommendations();
    CompletableFuture<List<Item>> viewedItemsCF = getViewedItems();
    CompletableFuture<List<Customer>> customerCF = getCustomer();   

  return  CompletableFuture.allOf(recommendationsCF,viewedItemsCF,customerCF).thenApply((voidcf) -> ok(
            views.html.home.render(
                    recommendationsCF.join(),
                    viewedItemsCF.join(),
                    customerCF.join())
    ));