Skip to content

Commit

Permalink
Implement Virtual Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-w1lde committed Nov 6, 2024
1 parent 1d8f397 commit c52e2c8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.furizon.backend.infrastructure.configuration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.support.TaskExecutorAdapter;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executors;

@EnableAsync
@Configuration
@ConditionalOnProperty(
value = "spring.thread-executor",
havingValue = "virtual"
)
public class ThreadConfig {
@Bean
public AsyncTaskExecutor applicationTaskExecutor() {
return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
}

@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> {
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SecurityThreadConfiguration {
@Bean(SESSION_THREAD_POOL_TASK_EXECUTOR)
public Executor sessionThreadPoolTaskExecutor() {
final var corePoolUpdateSize = securityConfig.getSession().getCorePoolUpdateSize();
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
final var executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolUpdateSize);
executor.setMaxPoolSize(corePoolUpdateSize);
executor.setThreadNamePrefix("session-thread-");
Expand Down
4 changes: 4 additions & 0 deletions application/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ server:
port: ${BACKEND_SERVER_PORT:9090}

spring:
threads:
virtual:
enabled: true
thread-executor: virtual
jpa:
open-in-view: false
application:
Expand Down

0 comments on commit c52e2c8

Please sign in to comment.