ThreadPoolTaskExecutorConfig.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.its.api.config;
  2. import lombok.Data;
  3. import lombok.EqualsAndHashCode;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
  8. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  9. import javax.annotation.PostConstruct;
  10. import java.util.concurrent.Executor;
  11. @EqualsAndHashCode(callSuper = true)
  12. @Slf4j
  13. @Data
  14. @Configuration
  15. public class ThreadPoolTaskExecutorConfig extends AsyncConfigurerSupport {
  16. private int poolCore = 0;
  17. @PostConstruct
  18. private void init() {
  19. this.poolCore = Runtime.getRuntime().availableProcessors();
  20. if (this.poolCore <= 8) {
  21. this.poolCore = 16;
  22. }
  23. log.info("[{}] ----------------------------", getClass().getSimpleName());
  24. log.info("[{}] availableProcessors: {} EA", getClass().getSimpleName(), Runtime.getRuntime().availableProcessors());
  25. log.info("[{}] PoolCore: {} EA", getClass().getSimpleName(), this.poolCore);
  26. }
  27. public ThreadPoolTaskExecutor getDefaultExecutor(int poolSize) {
  28. ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
  29. threadPoolTaskExecutor.setCorePoolSize(poolSize); // 인스턴스 되면서 기본적으로 띄울 스레드 개수.
  30. // 아무작업이 없어도 corePoolSize 만큼 스레드가 생성
  31. threadPoolTaskExecutor.setMaxPoolSize(poolSize*2); // 풀 최대개수, Queue Capacity 까지 꽉차는 경우 maxPoolSize 만큼 넓혀감
  32. threadPoolTaskExecutor.setQueueCapacity(poolSize*4); // 스레드 대기큐, Queue Capacity 가 꽉차면 스레드가 추가로 생성됨. Async 처리시 Queue Size
  33. // (설정하지 않으면 Integer.MAX 이기 때문에 성능에 문제가 발생함)
  34. return threadPoolTaskExecutor;
  35. }
  36. @Bean(name="cctvPsetScnrExecutor")
  37. public Executor getCctvPsetScnrExecutor() {
  38. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.poolCore);
  39. threadPoolTaskExecutor.setThreadNamePrefix("pset-scnr-pool-");
  40. threadPoolTaskExecutor.initialize();
  41. return threadPoolTaskExecutor;
  42. }
  43. @Bean(name="centerCommExecutor")
  44. public Executor getCenterCommExecutor() {
  45. ThreadPoolTaskExecutor threadPoolTaskExecutor = this.getDefaultExecutor((int)(this.poolCore/2));
  46. threadPoolTaskExecutor.setThreadNamePrefix("udp-comm-pool-");
  47. threadPoolTaskExecutor.initialize();
  48. return threadPoolTaskExecutor;
  49. }
  50. @Bean(name="schJobExecutor")
  51. public Executor getSchJobExecutor() {
  52. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.poolCore);
  53. threadPoolTaskExecutor.setThreadNamePrefix("sch-job-pool-");
  54. threadPoolTaskExecutor.initialize();
  55. return threadPoolTaskExecutor;
  56. }
  57. @Override
  58. public Executor getAsyncExecutor() {
  59. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.poolCore *2);
  60. threadPoolTaskExecutor.setThreadNamePrefix("async-pool-");
  61. threadPoolTaskExecutor.initialize();
  62. return threadPoolTaskExecutor;
  63. }
  64. }