ThreadPoolInitializer.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.its.bis.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 = Math.max(8, Runtime.getRuntime().availableProcessors());
  28. if (this.comm <= 0) {
  29. this.comm = MAX_CORE;
  30. }
  31. if (this.stat <= 0) {
  32. this.stat = MAX_CORE;
  33. }
  34. if (this.work <= 0) {
  35. this.work = MAX_CORE;
  36. }
  37. if (this.dbms <= 0) {
  38. this.dbms = MAX_CORE*4;
  39. }
  40. if (this.ping <= 0) {
  41. this.ping = MAX_CORE;
  42. }
  43. log.info("{}", this);
  44. }
  45. public ThreadPoolTaskExecutor getDefaultExecutor(int poolSize) {
  46. ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
  47. threadPoolTaskExecutor.setCorePoolSize(poolSize); // 인스턴스 되면서 기본적으로 띄울 스레드 개수.
  48. // 아무작업이 없어도 corePoolSize 만큼 스레드가 생성
  49. threadPoolTaskExecutor.setMaxPoolSize(poolSize*2); // 풀 최대개수, Queue Capacity 까지 꽉차는 경우 maxPoolSize 만큼 넓혀감
  50. threadPoolTaskExecutor.setQueueCapacity(1000); // 스레드 대기큐, Queue Capacity 가 꽉차면 스레드가 추가로 생성됨. Async 처리시 Queue Size
  51. // (설정하지 않으면 Integer.MAX 이기 때문에 성능에 문제가 발생함)
  52. return threadPoolTaskExecutor;
  53. }
  54. @Bean(name="centerCommExecutor")
  55. public Executor getCenterCommExecutor() {
  56. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.comm);
  57. threadPoolTaskExecutor.setThreadNamePrefix("comm-pool-");
  58. threadPoolTaskExecutor.initialize();
  59. return threadPoolTaskExecutor;
  60. }
  61. @Bean(name="dbmsDataExecutor")
  62. public Executor getDbmsDataExecutor() {
  63. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.dbms);
  64. threadPoolTaskExecutor.setThreadNamePrefix("dbms-pool-");
  65. threadPoolTaskExecutor.initialize();
  66. return threadPoolTaskExecutor;
  67. }
  68. @Bean(name="workDataExecutor")
  69. public Executor getWorkDataExecutor() {
  70. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.work);
  71. threadPoolTaskExecutor.setThreadNamePrefix("work-pool-");
  72. threadPoolTaskExecutor.initialize();
  73. return threadPoolTaskExecutor;
  74. }
  75. @Bean(name="statisticsExecutor")
  76. public Executor getStatisticsExecutor() {
  77. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.stat);
  78. threadPoolTaskExecutor.setThreadNamePrefix("stat-pool-");
  79. threadPoolTaskExecutor.initialize();
  80. return threadPoolTaskExecutor;
  81. }
  82. @Bean(name="icmpPingExecutor")
  83. public Executor getIcmpPingExecutor() {
  84. ThreadPoolTaskExecutor threadPoolTaskExecutor = getDefaultExecutor(this.ping);
  85. threadPoolTaskExecutor.setThreadNamePrefix("icmp-pool-");
  86. threadPoolTaskExecutor.initialize();
  87. return threadPoolTaskExecutor;
  88. }
  89. }