Based on my research, virtual threads in spring work in web applications (with embedded Tomcat or Jetty), however I didn't find examples of virtual threads in schedulers.
My request may not make sense, but there were specific situations that led me to open this ticket.
The application I am creating has the following characteristics: - a scheduler to run in the background - not being a web application, I don't need a server port - use virtual threads
With virtual threads enabled in my project, the scheduler runs just once and finishes immediately.
I tried to fix it as follows: - add a config: spring.main.web-application-type=NONE - I excluded tomcat from spring-boot-starter-web - I changed spring-boot-starter-web to spring-boot-starter - I modified my main method in several ways to try to adjust the problem
Then I realized that the schedule only worked correctly (without stopping) using the configuration below in the main method (CountDownLatch):
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ClosureThreadsSchedApplication.class, args);
new CountDownLatch(1).await(); // this line was added
}
I didn't like the implementation above, I think it's not a good idea to keep the code this way, I removed CountDownLatch.
I went to investigate the next attempt: and then I disabled the virtual threads in the application.yml and the scheduler started working correctly again, that is, the problem only occurs when the virtual threads are enabled.
With this, I would like to understand whether virtual threads in spring do not work with scheduler and non-web projects, because as in the example I explained above, the project seems to have its life cycle damaged and terminated unexpectedly.
Comment From: bclozel
Duplicates https://github.com/spring-projects/spring-boot/issues/37736
Comment From: jsqia
tks @bclozel