package com.tsi.comm.server.config; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import javax.annotation.PostConstruct; @Slf4j @Data @Configuration @ConfigurationProperties(prefix = "application.scheduling") public class SchedulingConfig implements SchedulingConfigurer { private int poolSize = 0; private final int MIN_POOL_SIZE = 10; private final int MAX_POOL_SIZE = 20; // 40코어라도 20개까지만 사용 @PostConstruct private void init() { if (this.poolSize <= 0) { int availableProcessors = Runtime.getRuntime().availableProcessors(); this.poolSize = Math.max(this.MIN_POOL_SIZE, availableProcessors); this.poolSize = Math.min(this.poolSize, this.MAX_POOL_SIZE); } MDC.put("filename", "config"); log.info("[SchedulingConfig] ------------"); log.info("[SchedulingConfig] poolCore: {} EA.", this.poolSize); MDC.clear(); } @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.setPoolSize(this.poolSize); threadPoolTaskScheduler.setThreadNamePrefix("tsi-scheduler-"); threadPoolTaskScheduler.initialize(); scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler); } }