With Spring one can implement Thread Pool simply by org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
1) Create ThreadPoolConfig as below -
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class ThreadPoolConfig {
@Bean
public ThreadPoolTaskExecutor threadPoolExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(50);
executor.setMaxPoolSize(1000);
executor.setAwaitTerminationSeconds(30);
executor.setPrestartAllCoreThreads(true);
// executor.initialize();
return executor;
}
}
2) Autowire ThreadPoolTaskExecutor where needed -
@Autowired
public ThreadPoolTaskExecutor taskExecutor;
logger.info("Active Threads: {}, Pool Size: {}, Queue Size: {}", taskExecutor.getActiveCount(),
taskExecutor.getPoolSize(), taskExecutor.getQueueSize());
Comments
Post a Comment