I'm building some simple web applications where I want to take advantage of both native compilation and virtual threads. So far with spring-boot 3.2.0 I can build native executables or take advantage of virtual threads, but not both:
env:
uname -a
Linux kl-ubuntu 6.2.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 6 10:23:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
java -version
java version "21" 2023-09-19
Java(TM) SE Runtime Environment Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)
sample project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>gvm_virtual_threads</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
the only class:
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
private final static Logger logger = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/greeting")
public String greet() {
logger.info("Inside greeting");
return "Hi!";
}
}
compiling:
mvn clean -Pnative native:compile
executing jvm without virtual threads and consuming it
java -jar target/gvm_virtual_threads-0.0.1.jar &
curl localhost:8080/greeting
log:
2023-11-25T22:02:03.255-03:00 INFO 2580376 --- [nio-8080-exec-3] com.example.demo.DemoApplication : Inside greeting
executing jvm with virtual threads and consuming it
java -jar target/gvm_virtual_threads-0.0.1.jar --spring.threads.virtual.enabled=true &
curl localhost:8080/greeting
log:
2023-11-25T22:05:28.564-03:00 INFO 2580516 --- [omcat-handler-0] com.example.demo.DemoApplication : Inside greeting
I can see that the thread name changed from nio-8080-exec-3
to omcat-handler-0
But with graalvm native compilation both
./target/gvm_virtual_threads &
curl localhost:8080/greeting
and
./target/gvm_virtual_threads --spring.threads.virtual.enabled=true &
curl localhost:8080/greeting
logs the same thread name:
2023-11-25T22:09:05.675-03:00 INFO 2580647 --- [nio-8080-exec-3] com.example.demo.DemoApplication : Inside greeting
I know this isn't the best way to corroborate if virtual threads is on, but when stressing the app and querying micrometer metrics via actuator I can see no reduction in jvm_threads_peak_threads
on graalvm compilations, when in jvm compilations there is.
Is this not an intended use case? Am I doing something wrong?
Setting spring.threads.virtual.enabled=true
changes the beans that are in the application context. The closed-world assumptions of GraalVM and AOT processing do not allow the beans that are in the application context to be modified at runtime.
To use virtual threads in a native image, set spring.threads.virtual.enabled=true
in your application.properties
file and then build the image.