Virtual Threads and Spring WebFlux

3.2k views Asked by At

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

1

There are 1 answers

0
jwpol On BEST ANSWER

Your code will not work in the current 3.5.11 version of project-reactor, therefore, also in spring-webflux because for blocking operations you are forced to use boundedElastic scheduler. If you use another pool you'll get IllegalStateException I would expect a dedicated Scheduler 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