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

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

 更新時(shí)間:2022年02月17日 16:17:04   作者:牛奮lch  
這篇文章主要介紹了spring boot使用自定義配置的線程池執(zhí)行Async異步任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在前面的博客中,//m.fzitv.net/article/106718.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; 
 } 
} 

三、在主類中開(kāi)啟配置支持

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} ) // 開(kāi)啟配置屬性支持 
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即配置線程池的方法名,此處如果不寫(xiě)自定義線程池的方法名,會(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; // 配置屬性類,見(jiàn)上面的代碼 
 
 @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即可。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中并發(fā)Queue種類與各自API特點(diǎn)以及使用場(chǎng)景說(shuō)明

    java中并發(fā)Queue種類與各自API特點(diǎn)以及使用場(chǎng)景說(shuō)明

    這篇文章主要介紹了java中并發(fā)Queue種類與各自API特點(diǎn)以及使用場(chǎng)景說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot的API文檔生成工具SpringDoc使用詳解

    SpringBoot的API文檔生成工具SpringDoc使用詳解

    這篇文章主要為大家介紹了SpringBoot的API文檔生成工具SpringDoc使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • SpringBoot自動(dòng)裝配之@Enable深入講解

    SpringBoot自動(dòng)裝配之@Enable深入講解

    這篇文章主要介紹了SpringBoot自動(dòng)裝配之@Enable,SpringBoot中提供了很多Enable開(kāi)頭的注解,這些注解都是用于動(dòng)態(tài)啟用某些功能的。而其底層原理是使用@Import注?解導(dǎo)入一些配置類,實(shí)現(xiàn)Bean的動(dòng)態(tài)加載
    2023-01-01
  • Spring?Boot配置內(nèi)容加密實(shí)現(xiàn)敏感信息保護(hù)

    Spring?Boot配置內(nèi)容加密實(shí)現(xiàn)敏感信息保護(hù)

    之前我們講過(guò)的配置相關(guān)知識(shí)都是Spring?Boot原生就提供的,而今天我們將介紹的功能并非Spring?Boot原生就支持,但卻非常有用:配置內(nèi)容的加密
    2021-11-11
  • MyBatis的通俗理解:SqlSession.getMapper()源碼解讀

    MyBatis的通俗理解:SqlSession.getMapper()源碼解讀

    這篇文章主要介紹了MyBatis的通俗理解:SqlSession.getMapper()源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過(guò)程解析

    Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過(guò)程解析

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • springboot掃描自定義的servlet和filter代碼詳解

    springboot掃描自定義的servlet和filter代碼詳解

    本文是一篇根據(jù)作者工作經(jīng)歷總結(jié)出來(lái)的關(guān)于springboot掃描自定義的servlet和filter代碼詳解的文章,小編覺(jué)得非常不錯(cuò),這里給大家分享下,和朋友們一起學(xué)習(xí),進(jìn)步。
    2017-10-10
  • 一文學(xué)會(huì)如何在SpringBoot中使用線程池執(zhí)行定時(shí)任務(wù)

    一文學(xué)會(huì)如何在SpringBoot中使用線程池執(zhí)行定時(shí)任務(wù)

    在開(kāi)發(fā)現(xiàn)代應(yīng)用程序時(shí),定時(shí)任務(wù)是一項(xiàng)常見(jiàn)的需求,SpringBoot提供了一個(gè)強(qiáng)大的定時(shí)任務(wù)框架,可以輕松地執(zhí)行各種定時(shí)任務(wù),結(jié)合線程池的使用,可以更好地管理任務(wù)的執(zhí)行,提高系統(tǒng)的性能和穩(wěn)定性,本文將介紹如何在Spring Boot中使用線程池執(zhí)行定時(shí)任務(wù)
    2023-06-06
  • Spring boot按日切分spring boot的nohup.out日志文件的方法

    Spring boot按日切分spring boot的nohup.out日志文件的方法

    過(guò)大的日志文件維護(hù)起來(lái)存在諸多問(wèn)題,所以最好是能夠按日或按大小切分日志文件,下面小編給大家?guī)?lái)了Spring boot按日切分spring boot的nohup.out日志文件的方法,一起看看吧
    2018-08-08
  • 淺談spring aop的五種通知類型

    淺談spring aop的五種通知類型

    這篇文章主要介紹了淺談spring aop的五種通知類型,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論

长治市| 石楼县| 兰州市| 通许县| 塔城市| 大同市| 循化| 永济市| 静安区| 保德县| 安西县| 勐海县| 彭泽县| 二连浩特市| 龙游县| 中宁县| 重庆市| 亳州市| 板桥市| 潮安县| 禹城市| 珲春市| 金阳县| 海伦市| 镇康县| 上蔡县| 绥德县| 调兵山市| 天峻县| 定州市| 曲松县| 鹤山市| 香港| 邢台市| 明溪县| 余庆县| 大悟县| 日土县| 滦南县| 松江区| 繁昌县|