5 changed files with 114 additions and 2 deletions
			
			
		| @ -0,0 +1,49 @@ | |||
| package com.epmet.config; | |||
| 
 | |||
| import com.epmet.properties.ThreadProperties; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.scheduling.annotation.EnableAsync; | |||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |||
| 
 | |||
| import java.util.concurrent.Executor; | |||
| import java.util.concurrent.ExecutorService; | |||
| import java.util.concurrent.ThreadPoolExecutor; | |||
| 
 | |||
| /** | |||
|  * 线程池配置类 | |||
|  */ | |||
| @Configuration | |||
| @EnableConfigurationProperties(ThreadProperties.class) | |||
| @EnableAsync | |||
| public class AsyncConfig { | |||
| 
 | |||
|     @Autowired | |||
|     private ThreadProperties threadProperties; | |||
| 
 | |||
|     @Bean | |||
|     public Executor executor() { | |||
|         ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); | |||
| 
 | |||
|         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | |||
|         executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); | |||
|         executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); | |||
|         executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); | |||
|         executor.setThreadNamePrefix("epmet-message-"); | |||
|         // rejection-policy:当pool已经达到max size的时候,如何处理新任务
 | |||
|         // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
 | |||
|         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
 | |||
|         executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); | |||
|         executor.initialize(); | |||
|         return executor; | |||
|     } | |||
| 
 | |||
|     @Bean | |||
|     public ExecutorService executorService() { | |||
|         ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); | |||
|         return executor.getThreadPoolExecutor(); | |||
|     } | |||
| 
 | |||
| } | |||
| @ -0,0 +1,25 @@ | |||
| package com.epmet.properties; | |||
| 
 | |||
| import lombok.Data; | |||
| import org.springframework.boot.context.properties.ConfigurationProperties; | |||
| 
 | |||
| /** | |||
|  * 线程池属性类 | |||
|  */ | |||
| @ConfigurationProperties(prefix = "thread") | |||
| @Data | |||
| public class ThreadProperties { | |||
| 
 | |||
|     private ThreadPoolProperties threadPool; | |||
| 
 | |||
|     @Data | |||
|     public static class ThreadPoolProperties { | |||
|         private int corePoolSize; | |||
|         private int maxPoolSize; | |||
|         private int queueCapacity; | |||
|         private int keepAlive; | |||
| 
 | |||
|         public ThreadPoolProperties() { | |||
|         } | |||
|     } | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue