【问题标题】:Java - Using Several Completion Stage to form one response jsonJava - 使用几个完成阶段来形成一个响应 json
【发布时间】:2018-01-30 05:33:13
【问题描述】:

我不确定我是否走对了路,但目前我卡住了。 我想要实现的是我想根据其父类别获取项目列表。

ParentCategory --< Category --< Item

而且对 CompletionStage 还不是很熟悉。

所以这里我有 2 个查询。第一个查询是获取 ParentCategory 的列表。然后我将遍历该列表以获取每个 ParentCategory 的项目列表

    ParentCategoryDao {
        findParentCategoriesByShop(int shopId);
    }

    ItemDao {
        //joined query of item and child category
        findItemByChildCategory(final int parentCategoryId)

    }

在控制器中:

    public CompletionStage<List<ProcessClass>> retrieveItems(final int shopId) {


        parentCategoryDao.findParentCategoriesByShop(shopId).thenApplyAsync(parentCategoryStream ->{

            ParentCategoryJson parentCategoryJson = new ParentCategoryJson();

            for(ParentCategory parentCategory : parentCategoryStream.collect(Collectors.toList())) {

                processClassJson.setProcessClassId(parentCategory.getId());
                processClassJson.setProcessClassName(processClass.getProcessClass());

                itemDao.findItemByChildCategory(parentCategory.getId()).thenApplyAsync(itemStream ->{
                    // Do operations on forming a list of items

                }, ec.current());


                //then maybe after is something like
                processClassJson.setItemList(itemList);

            }


        },ec.current())

    }

顺便说一句,我正在使用 Play Framework。 非常感谢任何帮助。

【问题讨论】:

  • DAO 接口的声明不完整(缺少返回类型),并且您正在使用未声明的ProcessClass 类型和processClassprocessClassJsonitemList 变量。你到底有什么问题?您尝试了哪些方法,为什么不起作用?
  • 其中的所有内容仅用于说明目的。
  • 没关系,但您应该将其设为minimal reproducible example,并明确指出您的问题所在。

标签: java java-8 completable-future


【解决方案1】:

令人惊讶的是,您没有在 thenApplyAsync 块中返回任何内容。此外,非阻塞函数只能从 thenCompose 调用以避免嵌套的 CompletionStage。不管怎样,下面的代码应该可以解决你的问题。

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;

....

  public static CompletionStage<List<Object>> retrieveItems(final int shopId) {
    return parentCategoryDao
        .findParentCategoriesByShop(shopId)
        .thenComposeAsync(
            stream ->
                resultsOfAllFutures(
                    stream
                        .map(
                            parentCategory ->
                                itemDao
                                    .findItemByChildCategory(parentCategory.getId())
                                    .thenApplyAsync(
                                        itemStream -> {
                                          // Do operations on forming a list of items
                                          return null;
                                        }))
                        .collect(Collectors.toList())));
  }

      public static <T> CompletableFuture<List<T>> resultsOfAllFutures(
          List<CompletionStage<T>> completionStages) {
        return CompletableFuture.completedFuture(null)
            .thenApply(
                __ ->
                    completionStages
                        .stream()
                        .map(future -> future.toCompletableFuture().join())
                        .collect(Collectors.toList()));
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多