ThreadPoolInitializer.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.its.pis.config;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
  9. import org.springframework.scheduling.annotation.EnableAsync;
  10. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  11. import javax.annotation.PostConstruct;
  12. import java.util.concurrent.Executor;
  13. @Slf4j
  14. @Getter
  15. @Setter
  16. @EnableAsync
  17. @Configuration
  18. @ConfigurationProperties(prefix = "application.thread-pool")
  19. public class ThreadPoolInitializer extends AsyncConfigurerSupport {
  20. private int comm = 0;
  21. private int stat = 0;
  22. private int work = 0;
  23. private int dbms = 0;
  24. private int ping = 0;
  25. @PostConstruct
  26. private void init() {
  27. int MAX_CORE = Runtime.getRuntime().availableProcessors();
  28. if (MAX_CORE < 8)
  29. MAX_CORE = 16;
  30. if (this.comm <= 0) {
  31. this.comm = MAX_CORE;
  32. }
  33. if (this.stat <= 0) {
  34. this.stat = MAX_CORE;
  35. }
  36. if (this.work <= 0) {
  37. this.work = MAX_CORE;
  38. }
  39. if (this.dbms <= 0) {
  40. this.dbms = MAX_CORE;
  41. }
  42. if (this.ping <= 0) {
  43. this.ping = MAX_CORE;
  44. }
  45. log.info("{}", this);
  46. }
  47. public ThreadPoolTaskExecutor getDefaultExecutor(int poolSize) {
  48. ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
  49. threadPoolTaskExecutor.setCorePoolSize(poolSize); // 인스턴스 되면서 기본적으로 띄울 스레드 개수.
  50. // 아무작업이 없어도 corePoolSize 만큼 스레드가 생성
  51. threadPoolTaskExecutor.setMaxPoolSize(poolSize*2); // 풀 최대개수, Queue Capacity 까지 꽉차는 경우 maxPoolSize 만큼 넓혀감
  52. threadPoolTaskExecutor.setQueueCapacity(1000); // 스레드 대기큐, Queue Capacity 가 꽉차면 스레드가 추가로 생성됨. Async 처리시 Queue Size
  53. // (설정하지 않으면 Integer.MAX 이기 때문에 성능에 문제가 발생함)
  54. return threadPoolTaskExecutor;
  55. }
  56. @Bean(name="centerCommExecutor")
  57. public Executor getCenterCommExecutor() {
  58. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.comm);
  59. threadPoolTaskExecutor.setThreadNamePrefix("comm-pool-");
  60. threadPoolTaskExecutor.initialize();
  61. return threadPoolTaskExecutor;
  62. }
  63. @Bean(name="dbmsDataExecutor")
  64. public Executor getDbmsDataExecutor() {
  65. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.dbms);
  66. threadPoolTaskExecutor.setThreadNamePrefix("dbms-pool-");
  67. threadPoolTaskExecutor.initialize();
  68. return threadPoolTaskExecutor;
  69. }
  70. @Bean(name="workDataExecutor")
  71. public Executor getWorkDataExecutor() {
  72. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.work);
  73. threadPoolTaskExecutor.setThreadNamePrefix("work-pool-");
  74. threadPoolTaskExecutor.initialize();
  75. return threadPoolTaskExecutor;
  76. }
  77. @Bean(name="statisticsExecutor")
  78. public Executor getStatisticsExecutor() {
  79. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.stat);
  80. threadPoolTaskExecutor.setThreadNamePrefix("stat-pool-");
  81. threadPoolTaskExecutor.initialize();
  82. return threadPoolTaskExecutor;
  83. }
  84. @Bean(name="icmpPingExecutor")
  85. public Executor getIcmpPingExecutor() {
  86. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.ping);
  87. threadPoolTaskExecutor.setThreadNamePrefix("icmp-pool-");
  88. threadPoolTaskExecutor.initialize();
  89. return threadPoolTaskExecutor;
  90. }
  91. }