I face some strange behavior while using completableFutures See my code snippet below for easy and better understanding. I iterate through list of messages and call handleMessage on each message. handleMessage method first calls getDataContentIdByService and finally mappingService.process method is called.
But the issue is once getDataContentIdByService method processing is done, before execution of mappingService.process method is complete, the call is returned back to whenComplete stage.
What i want is both getDataContentIdByService and mappingService.process methods should finish the execution in sequence and then whenComplete stage should be called.
Is something wrong in my code .. or? can someone help?
Completablefuture.allOf(messages.getList().stream()
.filter(this::msgOkOrLog)
.filter(this::notOnDenyList)
.map(msg -> handleMessage(msg, messages.getTrackingIdentifier(), messages.getMessageType()))
.toArray(CompletableFuture<?>[]::new))
.whenComplete((input, exception) -> countErrorsInContainer(messages));
The handleMessage function
protected CompletableFuture<Void> handleMessage(InternalProxyMessage message,
TrackingIdentifier containerTid, MessageType messageType) {
return getDataContentIdByService(message)
.thenAccept(message::setContentId)
.thenAccept(
mappingService.process(message)
.exceptionally(
ex -> {
throw new InternalServerErrorException("Unmanaged Error", ex);
}
})));
}