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
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?
message.md6.27KB
Was this page helpful?