乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      spring boot-執(zhí)行Async任務(wù)時(shí),使用自定義的線程池

       WindySky 2017-10-10

      在前面的博客中,http://blog.csdn.net/liuchuanhong1/article/details/54605697 我們使用了spring boot的異步操作,當(dāng)時(shí),我們使用的是默認(rèn)的線程池,但是,如果我們想根據(jù)項(xiàng)目來定制自己的線程池了,下面就來說說,如何定制線程池!

      一、增加配置屬性類

      1. package com.chhliu.springboot.async.configuration;  
      2.   
      3. import org.springframework.boot.context.properties.ConfigurationProperties;  
      4.   
      5. @ConfigurationProperties(prefix = "spring.task.pool") // 該注解的locations已經(jīng)被啟用,現(xiàn)在只要是在環(huán)境中,都會優(yōu)先加載  
      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. }  
      二、創(chuàng)建線程池

      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:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)  
      31.         // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來執(zhí)行  
      32.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
      33.         executor.initialize();  
      34.         return executor;  
      35.     }  
      36. }  
      三、在主類中開啟配置支持

      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. }  
      四、測試類

      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即配置線程池的方法名,此處如果不寫自定義線程池的方法名,會使用默認(rèn)的線程池  
      13.     public void doTask1(int i) throws InterruptedException{    
      14.         logger.info("Task"+i+" started.");    
      15.     }    
      16. }    
      五、測試

      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. }  
      測試結(jié)果如下:

      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.  

      測試結(jié)果ok!

      六、配置默認(rèn)的線程池

      如果我們想使用默認(rèn)的線程池,但是只是想修改默認(rèn)線程池的配置,那怎么做了,此時(shí)我們需要實(shí)現(xiàn)AsyncConfigurer類,示例代碼如下:

      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.  * 注意:該線程池被所有的異步任務(wù)共享,而不屬于某一個(gè)異步任務(wù) 
      17.  * 描述:配置異步任務(wù)的線程池 
      18.  * @author chhliu 
      19.  * 創(chuàng)建時(shí)間: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:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)    
      39.         // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來執(zhí)行    
      40.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
      41.         executor.initialize();    
      42.         return executor;    
      43.     }  
      44.   
      45.     @Override  
      46.     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {// 異步任務(wù)中異常處理  
      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. }  
      使用的時(shí)候,只需在方法上加上@Async即可。     

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多