最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年02月10日 10:03:07   作者:牛奮lch  
這篇文章主要介紹了spring boot使用自定義的線程池執(zhí)行Async任務(wù)的相關(guān)資料,需要的朋友可以參考下

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

一、增加配置屬性類

package com.chhliu.springboot.async.configuration; 
import org.springframework.boot.context.properties.ConfigurationProperties; 
@ConfigurationProperties(prefix = "spring.task.pool") // 該注解的locations已經(jīng)被啟用,現(xiàn)在只要是在環(huán)境中,都會(huì)優(yōu)先加載 
public class TaskThreadPoolConfig { 
 private int corePoolSize; 
 private int maxPoolSize; 
 private int keepAliveSeconds; 
 private int queueCapacity; 
 …………省略getter,setter方法………… 
} 

二、創(chuàng)建線程池

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:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù) 
 // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來(lái)執(zhí)行 
 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); 
 } 
} 

四、測(cè)試類

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

五、測(cè)試

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."); 
 } 
} 

測(cè)試結(jié)果如下:

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. 

測(cè)試結(jié)果ok!

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

如果我們想使用默認(rèn)的線程池,但是只是想修改默認(rèn)線程池的配置,那怎么做了,此時(shí)我們需要實(shí)現(xiàn)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; 
/** 
 * 注意:該線程池被所有的異步任務(wù)共享,而不屬于某一個(gè)異步任務(wù) 
 * 描述:配置異步任務(wù)的線程池 
 * @author chhliu 
 * 創(chuàng)建時(shí)間: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:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù) 
 // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來(lái)執(zhí)行 
 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); 
 executor.initialize(); 
 return executor; 
 } 
 @Override 
 public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {// 異步任務(wù)中異常處理 
 return new AsyncUncaughtExceptionHandler() { 
  @Override 
  public void handleUncaughtException(Throwable arg0, Method arg1, Object... arg2) { 
  log.error("=========================="+arg0.getMessage()+"=======================", arg0); 
  log.error("exception method:"+arg1.getName()); 
  } 
 }; 
 } 
} 

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

總結(jié)

以上所述是小編給大家介紹的spring boot使用自定義的線程池執(zhí)行Async任務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java實(shí)現(xiàn)拼圖游戲

    java實(shí)現(xiàn)拼圖游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 詳解Java面向?qū)ο笾械睦^承

    詳解Java面向?qū)ο笾械睦^承

    這篇文章主要介紹了詳解Java面向?qū)ο笾械睦^承,繼承提高代碼的復(fù)用性:通過(guò)繼承,子類可以直接使用父類的屬性和方法,不需要重復(fù)定義,需要的朋友可以參考下
    2023-05-05
  • Java多線程之搞定最后一公里詳解

    Java多線程之搞定最后一公里詳解

    Java 給多線程編程提供了內(nèi)置的支持。 一條線程指的是進(jìn)程中一個(gè)單一順序的控制流,一個(gè)進(jìn)程中可以并發(fā)多個(gè)線程,每條線程并行執(zhí)行不同的任務(wù),多線程是多任務(wù)的一種特別的形式,但多線程使用了更小的資源開銷
    2021-10-10
  • Spring如何消除代碼中的if-else/switch-case

    Spring如何消除代碼中的if-else/switch-case

    這篇文章主要給大家介紹了關(guān)于Spring如何消除代碼中if-else/switch-case的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java正則表達(dá)式之Pattern類實(shí)例詳解

    Java正則表達(dá)式之Pattern類實(shí)例詳解

    Pattern類的作用在于編譯正則表達(dá)式后創(chuàng)建一個(gè)匹配模式,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式之Pattern類的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • 詳解如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API

    詳解如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API

    作為后端程序員,我們的日常工作就是調(diào)用一些第三方服務(wù),將數(shù)據(jù)存入數(shù)據(jù)庫(kù),返回信息給前端。本文為大家介紹了如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API,需要的可以參考一下
    2022-12-12
  • Spring中@Configuration注解的使用場(chǎng)景

    Spring中@Configuration注解的使用場(chǎng)景

    這篇文章主要介紹了Spring中@Configuration注解的使用場(chǎng)景,@Configuration注解是從Spring?3.0版本開始加入的一個(gè)使Spring能夠支持注解驅(qū)動(dòng)開發(fā)的標(biāo)注型注解,主要用于標(biāo)注在類上,需要的朋友可以參考下
    2023-11-11
  • Java 反射獲取類詳細(xì)信息的常用方法總結(jié)

    Java 反射獲取類詳細(xì)信息的常用方法總結(jié)

    Java 反射獲取類詳細(xì)信息的常用方法總結(jié),需要的朋友可以參考一下
    2013-03-03
  • Java?hutool?List集合對(duì)象拷貝示例代碼

    Java?hutool?List集合對(duì)象拷貝示例代碼

    這篇文章主要介紹了Java?hutool?List集合對(duì)象拷貝的相關(guān)資料,文章還分享了在實(shí)現(xiàn)過(guò)程中遇到的一些問(wèn)題,并強(qiáng)調(diào)了閱讀源碼和正確配置CopyOptions的重要性,需要的朋友可以參考下
    2024-12-12
  • Java?不同版本的?Switch語(yǔ)句

    Java?不同版本的?Switch語(yǔ)句

    本文主要介紹了Java不同版本的Switch語(yǔ)句,自Java13以來(lái),Switch表達(dá)式就被添加到Java核心庫(kù)中,下面我們將介紹舊的Java?Switch語(yǔ)句和新的Switch語(yǔ)句的區(qū)別,需要的朋友可以參考一下
    2022-06-06

最新評(píng)論

台中县| 马龙县| 仁化县| 改则县| 松桃| 巴楚县| 湖州市| 德格县| 泽库县| 临潭县| 特克斯县| 松潘县| 胶州市| 万盛区| 乳源| 定兴县| 开阳县| 汉阴县| 雷州市| 大关县| 巴林左旗| 垫江县| 沙田区| 涡阳县| 鄂托克旗| 彭泽县| 哈尔滨市| 梧州市| 九龙县| 博罗县| 信宜市| 大田县| 灌云县| 无极县| 渭源县| 韶山市| 澄江县| 宣武区| 永和县| 霍林郭勒市| 永福县|