Multithread execution on Spring 4 using Reactor 2.0 Multithread execution on Spring 4 using Reactor 2.0 multithreading multithreading

Multithread execution on Spring 4 using Reactor 2.0


You should use Reactor's API. So instead of creating a Java Stream you should create Reactor Flux. Then you should use the flatMap function of a Flux in order to fetch some resources. If you want the resources to be in the same order as requested you can use flatMapSequential.

So the code would look like:

        Flux<Map<String,Object>> result = Flux.fromIterable(resources)                .flatMapSequential(resourceToMapFunction::apply)                .take(Duration.ofSeconds(5));

Then you have a Reactor stream and you can apply more operations to that stream. If you'd like to have simple list then you would have to block by using operator collectList. If you would like to have a control over a Thread on which given function will be executed then you'd have to get familiar with Scheduler.


React library will help a lot. Reactive programming (and async servlets) are supported by Spring 5, not Spring 4.

Spring 4 still supports some basic async features. It must be sufficient for you case. Here is a small example based on @EnableAsync and CompletableFuture:

https://dzone.com/articles/multi-threading-in-spring-boot-using-completablefu