spring boot-执行Async任务时,使用自定义的线程池

from:https://blog.csdn.net/liuchuanhong1/article/details/64132520

在前面的博客中,http://blog.csdn.net/liuchuanhong1/article/details/54605697 我们使用了spring boot的异步操作,当时,我们使用的是默认的线程池,但是,如果我们想根据项目来定制自己的线程池了,下面就来说说,如何定制线程池!

一、增加配置属性类

[java]  view plain  copy
  1. package com.chhliu.springboot.async.configuration;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4.   
  5. @ConfigurationProperties(prefix = "spring.task.pool"// 该注解的locations已经被启用,现在只要是在环境中,都会优先加载  
  6. public class TaskThreadPoolConfig {  
  7.     private int corePoolSize;  
  8.   
  9.     private int maxPoolSize;  
  10.   
  11.     private int keepAliveSeconds;  
  12.   
  13.     private int queueCapacity;  
  14.       
  15.     …………省略getter,setter方法…………  
  16. }  
二、创建线程池

[java]  view plain  copy
  1. package com.chhliu.springboot.async.pool;  
  2.   
  3. import java.util.concurrent.Executor;  
  4. import java.util.concurrent.ThreadPoolExecutor;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.context.annotation.Bean;  
  8. import org.springframework.context.annotation.Configuration;  
  9. import org.springframework.scheduling.annotation.EnableAsync;  
  10. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;  
  11.   
  12. import com.chhliu.springboot.async.configuration.TaskThreadPoolConfig;  
  13.   
  14. @Configuration  
  15. @EnableAsync  
  16. public class TaskExecutePool {  
  17.   
  18.     @Autowired  
  19.     private TaskThreadPoolConfig config;  
  20.   
  21.     @Bean  
  22.     public Executor myTaskAsyncPool() {  
  23.         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
  24.         executor.setCorePoolSize(config.getCorePoolSize());  
  25.         executor.setMaxPoolSize(config.getMaxPoolSize());  
  26.         executor.setQueueCapacity(config.getQueueCapacity());  
  27.         executor.setKeepAliveSeconds(config.getKeepAliveSeconds());  
  28.         executor.setThreadNamePrefix("MyExecutor-");  
  29.   
  30.         // rejection-policy:当pool已经达到max size的时候,如何处理新任务  
  31.         // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行  
  32.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
  33.         executor.initialize();  
  34.         return executor;  
  35.     }  
  36. }  
三、在主类中开启配置支持

[java]  view plain  copy
  1. package com.chhliu.springboot.async;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.context.properties.EnableConfigurationProperties;  
  6. import org.springframework.scheduling.annotation.EnableAsync;  
  7.   
  8. import com.chhliu.springboot.async.configuration.TaskThreadPoolConfig;  
  9.   
  10. @SpringBootApplication  
  11. @EnableAsync  
  12. @EnableConfigurationProperties({TaskThreadPoolConfig.class} ) // 开启配置属性支持  
  13. public class SpringbootAsyncApplication {  
  14.   
  15.     public static void main(String[] args) {  
  16.         SpringApplication.run(SpringbootAsyncApplication.class, args);  
  17.     }  
  18. }  
四、测试类

[java]  view plain  copy
  1. package com.chhliu.springboot.async.pool;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.scheduling.annotation.Async;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. @Component    
  9. public class AsyncTask {  
  10.     protected final Logger logger = LoggerFactory.getLogger(this.getClass());    
  11.         
  12.     @Async("myTaskAsyncPool")  //myTaskAsynPool即配置线程池的方法名,此处如果不写自定义线程池的方法名,会使用默认的线程池  
  13.     public void doTask1(int i) throws InterruptedException{    
  14.         logger.info("Task"+i+" started.");    
  15.     }    
  16. }    
五、测试

[java]  view plain  copy
  1. package com.chhliu.springboot.async;  
  2.   
  3. import java.util.concurrent.ExecutionException;  
  4.   
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import org.slf4j.Logger;  
  8. import org.slf4j.LoggerFactory;  
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.boot.test.context.SpringBootTest;  
  11. import org.springframework.test.context.junit4.SpringRunner;  
  12.   
  13. import com.chhliu.springboot.async.pool.AsyncTask;  
  14.   
  15. @RunWith(SpringRunner.class)  
  16. @SpringBootTest  
  17. public class SpringbootAsyncApplicationTests {  
  18.     protected final Logger logger = LoggerFactory.getLogger(this.getClass());  
  19.     @Autowired  
  20.     private AsyncTask asyncTask;  
  21.   
  22.     @Test  
  23.     public void AsyncTaskTest() throws InterruptedException, ExecutionException {  
  24.   
  25.         for (int i = 0; i < 100; i++) {  
  26.             asyncTask.doTask1(i);  
  27.         }  
  28.   
  29.         logger.info("All tasks finished.");  
  30.     }  
  31. }  
测试结果如下:

[java]  view plain  copy
  1. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-10] c.c.springboot.async.pool.AsyncTask      : Task60 started.  
  2. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-25] c.c.springboot.async.pool.AsyncTask      : Task61 started.  
  3. 2017-03-20 20:15:15.208  INFO 4068 --- [   MyExecutor-6] c.c.springboot.async.pool.AsyncTask      : Task62 started.  
  4. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-23] c.c.springboot.async.pool.AsyncTask      : Task63 started.  
  5. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-20] c.c.springboot.async.pool.AsyncTask      : Task64 started.  
  6. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-19] c.c.springboot.async.pool.AsyncTask      : Task65 started.  
  7. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-16] c.c.springboot.async.pool.AsyncTask      : Task66 started.  
  8. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-15] c.c.springboot.async.pool.AsyncTask      : Task67 started.  
  9. 2017-03-20 20:15:15.208  INFO 4068 --- [  MyExecutor-12] c.c.springboot.async.pool.AsyncTask      : Task68 started.  
  10. 2017-03-20 20:15:15.209  INFO 4068 --- [   MyExecutor-1] c.c.springboot.async.pool.AsyncTask      : Task69 started.  
  11. 2017-03-20 20:15:15.209  INFO 4068 --- [  MyExecutor-11] c.c.springboot.async.pool.AsyncTask      : Task81 started.  
  12. 2017-03-20 20:15:15.209  INFO 4068 --- [   MyExecutor-8] c.c.springboot.async.pool.AsyncTask      : Task82 started.  
  13. 2017-03-20 20:15:15.209  INFO 4068 --- [   MyExecutor-7] c.c.springboot.async.pool.AsyncTask      : Task83 started.  
  14. 2017-03-20 20:15:15.209  INFO 4068 --- [   MyExecutor-4] c.c.springboot.async.pool.AsyncTask      : Task84 started.  
  15. 2017-03-20 20:15:15.209  INFO 4068 --- [  MyExecutor-29] c.c.springboot.async.pool.AsyncTask      : Task85 started.  
  16. 2017-03-20 20:15:15.209  INFO 4068 --- [  MyExecutor-21] c.c.springboot.async.pool.AsyncTask      : Task86 started.  
  17. 2017-03-20 20:15:15.209  INFO 4068 --- [  MyExecutor-17] c.c.springboot.async.pool.AsyncTask      : Task88 started.  

测试结果ok!

六、配置默认的线程池

如果我们想使用默认的线程池,但是只是想修改默认线程池的配置,那怎么做了,此时我们需要实现AsyncConfigurer类,示例代码如下:

[java]  view plain  copy
  1. import java.lang.reflect.Method;  
  2. import java.util.concurrent.Executor;  
  3. import java.util.concurrent.ThreadPoolExecutor;  
  4.   
  5. import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.context.annotation.Configuration;  
  8. import org.springframework.scheduling.annotation.AsyncConfigurer;  
  9. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;  
  10.   
  11. import com.chhliu.cq.emailservice.threadconfiguration.TaskThreadPoolConfig;  
  12.   
  13. import lombok.extern.slf4j.Slf4j;    
  14.   
  15. /** 
  16.  * 注意:该线程池被所有的异步任务共享,而不属于某一个异步任务 
  17.  * 描述:配置异步任务的线程池 
  18.  * @author chhliu 
  19.  * 创建时间:2017年5月22日 上午10:20:56 
  20.  * @version 1.2.0 
  21.  */  
  22. @Slf4j  
  23. @Configuration  
  24. public class AsyncTaskExecutePool implements AsyncConfigurer{    
  25.     
  26.     @Autowired    
  27.     private TaskThreadPoolConfig config;  // 配置属性类,见上面的代码  
  28.     
  29.     @Override  
  30.     public Executor getAsyncExecutor() {  
  31.         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
  32.         executor.setCorePoolSize(config.getCorePoolSize());    
  33.         executor.setMaxPoolSize(config.getMaxPoolSize());    
  34.         executor.setQueueCapacity(config.getQueueCapacity());    
  35.         executor.setKeepAliveSeconds(config.getKeepAliveSeconds());    
  36.         executor.setThreadNamePrefix("taskExecutor-");    
  37.     
  38.         // rejection-policy:当pool已经达到max size的时候,如何处理新任务    
  39.         // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行    
  40.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
  41.         executor.initialize();    
  42.         return executor;    
  43.     }  
  44.   
  45.     @Override  
  46.     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {// 异步任务中异常处理  
  47.         return new AsyncUncaughtExceptionHandler() {  
  48.               
  49.             @Override  
  50.             public void handleUncaughtException(Throwable arg0, Method arg1, Object... arg2) {  
  51.                 log.error("=========================="+arg0.getMessage()+"=======================", arg0);  
  52.                 log.error("exception method:"+arg1.getName());  
  53.             }  
  54.         };  
  55.     }    
  56. }  
使用的时候,只需在方法上加上@Async即可。

猜你喜欢

转载自blog.csdn.net/u010142437/article/details/80668289