There is many ways how you can achieve parallel execution.
One way might be using Streams:
// First prepare tasks you want to run in parallel
List<Supplier<Result>> tasks = new ArrayList<>();
tasks.add(() -> {
Result result = // get result from WebClient
return result;
});
// Next execute in parallel all tasks and collect results
List<Result> results = tasks.parallelStream()
.map(Supplier::get)
.collect(Collectors.toList());
// Merge results into one
Of if you would to have finer control over threads (how many tasks can run in parallel at once, to not have to spawn new threads for every endpoint call etc), then since you are using Spring Boot you might use eg. AsyncTaskExecutor
class MyAwesomeController {
private final AsyncTaskExecutor executor;
public MyAwesomeController() {
executor = new TaskExecutorBuilder()
.corePoolSize(5) // how many threads to keep alive
.maxPoolSize(20) // how many max threads (taks) can be executed at one
// Some other settings (eg. how long to keep idle thread alive etc.
.build();
}
public Result endpoint() {
// First prepare tasks you want to run in parallel
List<Future<Result>> tasks = new ArrayList<>();
tasks.add(executor.submit(() -> {
Result result = // get result from WebClient
return result;
}));
// Next collect results from Future
List<Result> results = tasks.stream()
.map(Future::get)
.collect(Collectors.toList());
// Merge results into one
}
}
More info on executors you can find eg. here:
https://www.baeldung.com/java-executor-service-tutorial