スプリングブーツでマルチスレッドを使用するには、_@EnableAsync
_と_@Async
_注釈を使用しています。私はサービスA(fast)とサービスB(遅い)です。
さまざまなプールを設定できますか?そのため、Bの電話がたくさんある場合、アプリケーションはまだBから別のプールでサービスAを処理できます。
_@Configuration
@EnableAsync
public class ServiceExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(30);
taskExecutor.setMaxPoolSize(40);
taskExecutor.setQueueCapacity(10);
taskExecutor.initialize();
return taskExecutor;
}
}
_
@Bean(name = "threadPoolExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("threadPoolExecutor-");
executor.initialize();
return executor;
}
@Bean(name = "ConcurrentTaskExecutor")
public TaskExecutor taskExecutor2 () {
return new ConcurrentTaskExecutor(
Executors.newFixedThreadPool(3));
}
@Override
@Async("threadPoolExecutor")
public void createUserWithThreadPoolExecutor(){
System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
System.out.println("User created with thread pool executor");
}
@Override
@Async("ConcurrentTaskExecutor")
public void createUserWithConcurrentExecutor(){
System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
System.out.println("User created with concurrent task executor");
}
_