spring boot使用自訂配置的線程池執行Async非同步任務,springasync

來源:互聯網
上載者:User

spring boot使用自訂配置的線程池執行Async非同步任務,springasync

在前面的部落格中,http://www.bkjia.com/article/106718.htm 我們使用了spring boot的非同步作業,當時,我們使用的是預設的線程池,但是,如果我們想根據項目來定製自己的線程池了,下面就來說說,如何定製線程池!

一、增加配置屬性類
package com.chhliu.springboot.async.configuration; import org.springframework.boot.context.properties.ConfigurationProperties;  @ConfigurationProperties(prefix = "spring.task.pool") // 該註解的locations已經被啟用,現在只要是在環境中,都會優先載入 public class TaskThreadPoolConfig {  private int corePoolSize;   private int maxPoolSize;   private int keepAliveSeconds;   private int queueCapacity;    …………省略getter,setter方法………… } 
二、建立線程池
package com.chhliu.springboot.async.pool; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import org.springframework.beans.factory.annotation.Autowired; 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 com.chhliu.springboot.async.configuration.TaskThreadPoolConfig;  @Configuration @EnableAsync public class TaskExecutePool {   @Autowired  private TaskThreadPoolConfig config;   @Bean  public Executor myTaskAsyncPool() {   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();   executor.setCorePoolSize(config.getCorePoolSize());   executor.setMaxPoolSize(config.getMaxPoolSize());   executor.setQueueCapacity(config.getQueueCapacity());   executor.setKeepAliveSeconds(config.getKeepAliveSeconds());   executor.setThreadNamePrefix("MyExecutor-");    // rejection-policy:當pool已經達到max size的時候,如何處理新任務   // CALLER_RUNS:不在新線程中執行任務,而是由調用者所在的線程來執行   executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());   executor.initialize();   return executor;  } } 
三、在主類中開啟配置支援
package com.chhliu.springboot.async; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.scheduling.annotation.EnableAsync;  import com.chhliu.springboot.async.configuration.TaskThreadPoolConfig;  @SpringBootApplication @EnableAsync @EnableConfigurationProperties({TaskThreadPoolConfig.class} ) // 開啟配置屬性支援 public class SpringbootAsyncApplication {   public static void main(String[] args) {   SpringApplication.run(SpringbootAsyncApplication.class, args);  } } 
四、測試類別
package com.chhliu.springboot.async.pool;  import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component;  @Component public class AsyncTask {  protected final Logger logger = LoggerFactory.getLogger(this.getClass());    @Async("myTaskAsyncPool") //myTaskAsynPool即配置線程池的方法名,此處如果不寫自訂線程池的方法名,會使用預設的線程池  public void doTask1(int i) throws InterruptedException{   logger.info("Task"+i+" started.");  } } 
五、測試
package com.chhliu.springboot.async; import java.util.concurrent.ExecutionException; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;  import com.chhliu.springboot.async.pool.AsyncTask;  @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootAsyncApplicationTests {  protected final Logger logger = LoggerFactory.getLogger(this.getClass());  @Autowired  private AsyncTask asyncTask;   @Test  public void AsyncTaskTest() throws InterruptedException, ExecutionException {    for (int i = 0; i < 100; i++) {    asyncTask.doTask1(i);   }    logger.info("All tasks finished.");  } } 

測試結果如下:

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

測試結果ok!

六、配置預設的線程池

如果我們想使用預設的線程池,但是只是想修改預設線程池的配置,那怎麼做了,此時我們需要實現AsyncConfigurer類,範例程式碼如下:

import java.lang.reflect.Method; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import com.chhliu.cq.emailservice.threadconfiguration.TaskThreadPoolConfig; import lombok.extern.slf4j.Slf4j;  /**  * 注意:該線程池被所有的非同步任務共用,而不屬於某一個非同步任務  * 描述:配置非同步任務的線程池  * @author chhliu  * 建立時間:2017年5月22日 上午10:20:56  * @version 1.2.0  */ @Slf4j @Configuration public class AsyncTaskExecutePool implements AsyncConfigurer{   @Autowired  private TaskThreadPoolConfig config; // 配置屬性類,見上面的代碼   @Override  public Executor getAsyncExecutor() {   ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();   executor.setCorePoolSize(config.getCorePoolSize());   executor.setMaxPoolSize(config.getMaxPoolSize());   executor.setQueueCapacity(config.getQueueCapacity());   executor.setKeepAliveSeconds(config.getKeepAliveSeconds());   executor.setThreadNamePrefix("taskExecutor-");    // rejection-policy:當pool已經達到max size的時候,如何處理新任務   // CALLER_RUNS:不在新線程中執行任務,而是由調用者所在的線程來執行   executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());   executor.initialize();   return executor;  }   @Override  public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {// 非同步任務中異常處理   return new AsyncUncaughtExceptionHandler() {        @Override    public void handleUncaughtException(Throwable arg0, Method arg1, Object... arg2) {     log.error("=========================="+arg0.getMessage()+"=======================", arg0);     log.error("exception method:"+arg1.getName());    }   };  } } 

使用的時候,只需在方法上加上@Async即可。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.