You mentioned in your question the option spring.virtual.threads.enabled=true
. Just to clarify, you mean spring.threads.virtual.enabled=true
right?
Also, if you would like to return the response immediately, may I propose to change your code from:
@GetMapping
public ResponseEntity<String> hello() {
log.info("Received request in controller. Handled by thread: {}. Is Virtual Thread?: {}",
Thread.currentThread().getName(), Thread.currentThread().isVirtual());
helloService.hello();
return ResponseEntity.ok("Hello, World!");
}
to something like:
@GetMapping
public ResponseEntity<String> hello() {
log.info("Received request in controller. Handled by thread: {}. Is Virtual Thread?: {}",
Thread.currentThread().getName(), Thread.currentThread().isVirtual());
CompletableFuture.runAsync(() -> helloService.hello());
return ResponseEntity.ok("Hello, World!");
}
I tested on my local and this works.