我有以下代碼,它從回傳 CompletableFuture 的 Web API 獲取給定時間間隔的報告。如果超過回傳報告的行數,時間間隔將分為兩半,并為兩半呼叫 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));
如果我知道我做錯了什么,我將不勝感激。謝謝
uj5u.com熱心網友回復:
我將我的原始代碼轉換為這樣的東西,它似乎正在作業:
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);
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383350.html
