ExecutorConfig.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package cn.com.zyjblogs.chat.config.threadpool;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. import java.util.concurrent.Executor;
  8. import java.util.concurrent.ThreadPoolExecutor;
  9. /**
  10. * copyright (C), 2021, 北京同创永益科技发展有限公司
  11. *
  12. * @author zhuyijun
  13. * @version 1.0.0
  14. * <author> <time> <version> <description>
  15. * zhuyijun 2021/6/7 8:38 1.0 线程池
  16. * @program hatech-bcms-core
  17. * @description 线程池
  18. * @create 2021/6/7 8:38
  19. */
  20. @Configuration
  21. @Slf4j
  22. public class ExecutorConfig {
  23. @Value("${async.executor.thread.core_pool_size}")
  24. private int corePoolSize;
  25. @Value("${async.executor.thread.max_pool_size}")
  26. private int maxPoolSize;
  27. @Value("${async.executor.thread.queue_capacity}")
  28. private int queueCapacity;
  29. @Value("${async.executor.thread.name.prefix}")
  30. private String namePrefix;
  31. @Bean(name = "asyncServiceExecutor")
  32. public Executor asyncServiceExecutor() {
  33. log.info("start asyncServiceExecutor");
  34. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  35. //配置核心线程数
  36. executor.setCorePoolSize(corePoolSize);
  37. //配置最大线程数
  38. executor.setMaxPoolSize(maxPoolSize);
  39. //配置队列大小
  40. executor.setQueueCapacity(queueCapacity);
  41. //配置线程池中的线程的名称前缀
  42. executor.setThreadNamePrefix(namePrefix);
  43. // rejection-policy:当pool已经达到max size的时候,如何处理新任务
  44. // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
  45. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  46. //执行初始化
  47. executor.initialize();
  48. log.info("asyncServiceExecutor init over.....");
  49. return executor;
  50. }
  51. }