Recently Java 21 was released in which they added virtual threads, I was looking at some performance tests of Spring Boot on virtual threads and Spring WebFlux, the results showed that standard Spring Boot outperforms reactive Spring WebFlux, I have a question is there any point to use virtual threads in Spring WebFlux to improve performance? Will there be a performance gain? Will it not be a blocking operation? Also, how to use virtual threads in Spring WebFlux? Is my code below a good practice?
private final ExecutorService virtualPool;
public Mono<String> myMethod() {
return Mono.fromFuture(CompletableFuture.supplyAsync(() -> doBlockOperation(), virtualPool));
}
I want to use virtual threads in Spring WebFlux to get a performance boost
Your code will not work in the current
3.5.11
version of project-reactor, therefore, also inspring-webflux
because for blocking operations you are forced to useboundedElastic
scheduler. If you use another pool you'll getIllegalStateException
I would expect a dedicatedScheduler
in the next releases of the project reactor.You can track the integration between Project Reactor and Project Loom (Virtual Thrads) under https://github.com/reactor/reactor-core/issues/3084