我试图PUT在循环中调用rest api for request.每个电话都是一个CompletableFuture.每个api调用都返回一个类型的对象RoomTypes.RoomType
我想在不同的列表中收集响应(成功和错误响应).我如何实现这一目标?我确信我无法使用,allOf因为如果任何一个调用无法更新,它将无法获得所有结果.
如何记录每次通话的错误/异常?
public void sendRequestsAsync(Map<Integer, List> map1) {
List<CompletableFuture<Void>> completableFutures = new ArrayList<>(); //List to hold all the completable futures
List<RoomTypes.RoomType> responses = new ArrayList<>(); //List for responses
ExecutorService yourOwnExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (Map.Entry<Integer, List> entry :map1.entrySet()) {
CompletableFuture requestCompletableFuture = CompletableFuture
.supplyAsync(
() ->
//API call which returns object of type RoomTypes.RoomType
updateService.updateRoom(51,33,759,entry.getKey(),
new RoomTypes.RoomType(entry.getKey(),map2.get(entry.getKey()),
entry.getValue())),
yourOwnExecutor
)//Supply the task you wanna run, in your case http request …Run Code Online (Sandbox Code Playgroud)