SchedulingConfig.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.tsi.comm.server.config;
  2. import lombok.Data;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.slf4j.MDC;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  8. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  9. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  10. import javax.annotation.PostConstruct;
  11. @Slf4j
  12. @Data
  13. @Configuration
  14. @ConfigurationProperties(prefix = "application.scheduling")
  15. public class SchedulingConfig implements SchedulingConfigurer {
  16. private int poolSize = 0;
  17. private final int MIN_POOL_SIZE = 10;
  18. private final int MAX_POOL_SIZE = 20; // 40코어라도 20개까지만 사용
  19. @PostConstruct
  20. private void init() {
  21. if (this.poolSize <= 0) {
  22. int availableProcessors = Runtime.getRuntime().availableProcessors();
  23. this.poolSize = Math.max(this.MIN_POOL_SIZE, availableProcessors);
  24. this.poolSize = Math.min(this.poolSize, this.MAX_POOL_SIZE);
  25. }
  26. MDC.put("filename", "config");
  27. log.info("[SchedulingConfig] ------------");
  28. log.info("[SchedulingConfig] poolCore: {} EA.", this.poolSize);
  29. MDC.clear();
  30. }
  31. @Override
  32. public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
  33. ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
  34. threadPoolTaskScheduler.setPoolSize(this.poolSize);
  35. threadPoolTaskScheduler.setThreadNamePrefix("tsi-scheduler-");
  36. threadPoolTaskScheduler.initialize();
  37. scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
  38. }
  39. }