[英]Asynchronous api call using CompletableFuture in Java 8
Following is my use case that I am trying to implement using CompletableFuture class以下是我尝试使用 CompletableFuture 类实现的用例
I tried the following code but it is not working correctly我尝试了以下代码,但无法正常工作
// list content
List<Integer> ids = Arrays.asList(1,2,3,4,5);
// Api call using parallel stream - not sure how I can include a time limit here so that
// I can get partial list of updatedIds based on delay settings
List<Integer> updatedIds = ids.parallelStream().map(item -> {
// api call equivalent of increment 1
return item+1;
}).collect(Collectors.toList());
// Asynchronous api call using CompletableFuture class - not sure how I can
// dynamically call the function for all items in the ids list.
// Following is what I tried to do by reading
// https://www.baeldung.com/java-completablefuture
encounterIdSet.parallelStream().forEach(id -> {
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> serviceCall(id));
});
List<Integer> = completableFuture.get(60, TimeUnit.SECONDS); // may be in incorrect location
// I want to process the list of returned integers here - whatever I am getting in 60 seconds timeout mentioned in timeout settings
// service call definition
Integer serviceCall(id){
return id +1;
}
Can you please guide me on this use case?你能指导我这个用例吗? I need 1. timeout settings with 2. asynchronous data processing of 3. unknown number of items.我需要 1. 超时设置 2. 异步数据处理 3. 未知数量的项目。
I am using Java 8.我正在使用 Java 8。
Thanks.谢谢。
I can think of two ways, with some minor differences:我可以想到两种方法,但有一些细微差别:
List<CompletableFuture<Integer>> futures = ids.stream()
.map(id -> CompletableFuture.supplyAsync(() -> id + 1))
.collect(Collectors.toList());
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(60, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// log
}
List<Integer> result = futures
.stream()
.filter(future -> future.isDone() && !future.isCompletedExceptionally())
.map(CompletableFuture::join)
.collect(Collectors.toList());
or或者
List<Integer> result = ids.stream()
.map(id -> CompletableFuture.supplyAsync(() -> id + 1))
.map(cf -> {
try {
return cf.get(60, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// log
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.