Virtual threads

If I have the following implementation
@Configuration
@EnableAsync
public class VirtualThreadConfig {

@Bean
@Primary
public TaskExecutor virtualThreadExecutor() {
return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
}
}
@Configuration
@EnableAsync
public class VirtualThreadConfig {

@Bean
@Primary
public TaskExecutor virtualThreadExecutor() {
return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
}
}
Service
@Async
public List<UserDto> getAllAsync() {

UserDto[] users = restClient.get()
.uri(baseUrl + "/users")
.retrieve()
.body(UserDto[].class);
return List.of(users);
}
@Async
public List<UserDto> getAllAsync() {

UserDto[] users = restClient.get()
.uri(baseUrl + "/users")
.retrieve()
.body(UserDto[].class);
return List.of(users);
}
If Async already wraps the method in a CompletableFuture it is recommended not to rewrap it because it would be redundant. How I implement in the controller, I'm using java 21, version spring 3.5.0
@GetMapping
public CompletableFuture<ResponseEntity<List<UserDto>>> getAll() {

}
@GetMapping
public CompletableFuture<ResponseEntity<List<UserDto>>> getAll() {

}
13 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @pedroavila! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st7mo ago
Are you using a recent Spring version? If so, why are you not just enabling virtual threads in the application.properties and do everything synchronously?
pedroavila
pedroavilaOP7mo ago
version spring 3.5.0 have example the configuration that solved my problem?
dan1st
dan1st7mo ago
Spring Boot 3.5.0 supports virtual threads in the way I mentioned if you set it in the application.properties, Spring should use virtual threads wherever it can (and where it makes sense)
pedroavila
pedroavilaOP7mo ago
Is this VirtualThreadConfig implementation no longer necessary?
dan1st
dan1st7mo ago
no just add spring.threads.virtual.enabled to the application.properties and it configures everything necessary for virtual threads which is more than your config class
pedroavila
pedroavilaOP7mo ago
spring.threads.virtual.enabled=true

public List<UserDto> getAllAsync() {

UserDto[] users = restClient.get()
.uri(baseUrl + "/users")
.retrieve()
.body(UserDto[].class);
return List.of(users);
}

@GetMapping
public ResponseEntity<?> getAll() {
try {
List<UserDto> users = serviceUser.getAllAsync();

return ResponseEntity.ok(users);
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(500).body(null);
}
}
spring.threads.virtual.enabled=true

public List<UserDto> getAllAsync() {

UserDto[] users = restClient.get()
.uri(baseUrl + "/users")
.retrieve()
.body(UserDto[].class);
return List.of(users);
}

@GetMapping
public ResponseEntity<?> getAll() {
try {
List<UserDto> users = serviceUser.getAllAsync();

return ResponseEntity.ok(users);
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(500).body(null);
}
}
In this magical way I would already be working with virtual threads and my methods would be asynchronous and non-blocking.
dan1st
dan1st7mo ago
The code would be blocking but it doesn't block platform/OS threadsso you don't have the scalability issues/it's as if you'd write asynchronous code and I would rename the getAllAsync method
pedroavila
pedroavilaOP7mo ago
Wow, great how wonderful is spring 3+ and java 21 with the virtual threads very magical compared to the traditional threads that you had to configure the thread and work with CompletableFuture, the new implementation is cleaner
dan1st
dan1st7mo ago
yep
pedroavila
pedroavilaOP7mo ago
Thanks very @dan1st | Daniel
JavaBot
JavaBot7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.
JavaBot
JavaBot7mo ago
Post Closed
This post has been closed by <@558689208907202566>.

Did you find this page helpful?