Query: Understanding `CompletableFuture.anyOf()` Behavior — First Valid or Fastest Response?
Context:
In a task to validate element usage across multiple microservices (MSAs) before deletion, I use parallel API calls with
- Does
- Any best practices for handling parallel API calls with validation?
CompletableFuture
to speed up the process. I need to return as soon as I receive the first confirmation that the element is in use, but I’m unclear about the behavior of CompletableFuture.anyOf()
.
Refer to attached file for details.
The Issue:
- First Approach: Works initially, but after the first deletion, the ExecutorService
is shut down, causing issues with subsequent calls.
- Second Approach: Using CompletableFuture.anyOf()
to handle Feign calls. The question is:
- Does anyOf()
return the result of the first valid response (indicating the element is in use)?
- Or does it return based on the fastest call, regardless of validity?
Goal: Ensure execution stops and returns the first valid result, not just the fastest.
Question:- Does
CompletableFuture.anyOf()
prioritize the first valid response or just the fastest one?- Any best practices for handling parallel API calls with validation?
4 Replies
⌛
This post has been reserved for your question.
Hey @伏黒甚爾! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Is this a homework assignment?
Hi
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.