繁体   English   中英

收集 CompletableFuture 导致递归调用

[英]Collect CompletableFuture results in recursive calls

我有以下代码,即从 web API 获取给定时间间隔的报告,返回 CompletableFuture。 如果超过返回报告的行数,时间间隔将被分成两半,并且两半都会调用 API。 这将递归重复,直到行数满足条件。

我想通过这种方法获得CompletableFuture<List<APIResult>>甚至更好的List<CompletableFuture<APIResult>>

当不需要间隔拆分时,我的代码运行正常。 如果需要递归调用,它将只返回空列表,并且稍后会异步执行递归调用。

 protected CompletableFuture<List<APIResult<String>>> generateDomainReport(
        ExtendedDomainConfiguration domain,
        ZonedDateTime chunkStartDate,
        ZonedDateTime chunkEndDate,
        List<APIResult<String>> result) {

    CompletableFuture<APIResult<String>> response = runReport(domain.getDomainName(), chunkStartDate, chunkEndDate);
    return response.thenApply(report -> {
        // in case that count of report rows hit the limit, split timeWindow into two half,
        // calculate middle date and run report for both intervals: <startDate, middleDate>; <middleDate, endDate>
        if (isReportMaxRowCountReached(report.getContent(), domain.getMaxReportRowCount())) {
            LOGGER.warn(String.format(
                    "Report limit row counts was reached. " +
                            "Splitting window <%s, %s> into 2 parts and rerunning report",
                    chunkStartDate, chunkEndDate));
            Duration timeWindow = Duration.between(chunkStartDate, chunkEndDate);
            ZonedDateTime chunkMiddleDate = chunkEndDate.minus(Duration.ofSeconds(timeWindow.getSeconds() / 2));
            // recursively repeat until count of returned rows is under the limit
            generateDomainReport(domain, chunkStartDate, chunkMiddleDate, result);
            generateDomainReport(domain, chunkMiddleDate, chunkEndDate, result);
        } else {
            result.add(report);
        }
        return result;
    });
}

我也尝试过使用类似的东西,但没有帮助:

response
.thenAccept(r -> generateDomainReport(domain, chunkStartDate, chunkMiddleDate, result))
.thenAccept(r -> generateDomainReport(domain, chunkMiddleDate, chunkEndDate, result));

如果我知道我做错了什么,我将不胜感激。 谢谢

我将我的原始代码转换为这样的东西,它似乎正在工作:

protected CompletableFuture<List<APIResult<String>>> generateDomainReport(
            ExtendedDomainConfiguration domain,
            ZonedDateTime chunkStartDate,
            ZonedDateTime chunkEndDate) {

        CompletableFuture<APIResult<String>> response = runReport(domain.getDomainName(), chunkStartDate, chunkEndDate);
        return response.thenApplyAsync(report -> {
            // in case that count of report rows hit the limit, split timeWindow into two half,
            // calculate middle date and run report for both intervals: <startDate, middleDate>; <middleDate, endDate>
            if (isReportMaxRowCountReached(report.getContent(), domain.getMaxReportRowCount())) {
                LOGGER.warn(String.format(
                        "CSV report limit row counts was reached. " +
                                "Splitting window <%s, %s> into 2 parts and rerunning report",
                        chunkStartDate, chunkEndDate));
                Duration timeWindow = Duration.between(chunkStartDate, chunkEndDate);
                ZonedDateTime chunkMiddleDate = chunkEndDate.minus(Duration.ofSeconds(timeWindow.getSeconds() / 2));
                // recursively repeat until count of returned rows is under the limit
                return generateDomainReport(domain, chunkStartDate, chunkMiddleDate)
                        .thenCombineAsync(generateDomainReport(domain, chunkMiddleDate, chunkEndDate),
                                (result1, result2) -> {
                                    List<APIResult<String>> result = new ArrayList<>(result1.size() + result2.size());
                                    result.addAll(result1);
                                    result.addAll(result2);
                                    return result;
                                }).join();
            } else {
                return List.of(report);
            }
        });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM