亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

在前面的博客中,http://chabaoo.cn/article/134866.htm 我們使用了spring boot的異步操作,當時,我們使用的是默認的線程池,但是,如果我們想根據(jù)項目來定制自己的線程池了,下面就來說說,如何定制線程池!

一、增加配置屬性類

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

四、測試類

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

測試結(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. 

測試結(jié)果ok!

六、配置默認的線程池

如果我們想使用默認的線程池,但是只是想修改默認線程池的配置,那怎么做了,此時我們需要實現(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ù)共享,而不屬于某一個異步任務(wù) 
 * 描述:配置異步任務(wù)的線程池 
 * @author chhliu 
 * 創(chuàng)建時間: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已經(jīng)達到max size的時候,如何處理新任務(wù) 
 // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來執(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()); 
  } 
 }; 
 } 
} 

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

總結(jié)

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

相關(guān)文章

  • 用Java實現(xiàn)小球碰壁反彈的簡單實例(算法十分簡單)

    用Java實現(xiàn)小球碰壁反彈的簡單實例(算法十分簡單)

    下面小編就為大家?guī)硪黄肑ava實現(xiàn)小球碰壁反彈的簡單實例(算法十分簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • Java并發(fā)編程——volatile關(guān)鍵字

    Java并發(fā)編程——volatile關(guān)鍵字

    這篇文章主要介紹了Java并發(fā)編程——volatile關(guān)鍵字的相關(guān)資料,幫助大家更好的理解和學習Java并發(fā)編程,感興趣的朋友可以了解下
    2020-10-10
  • java異常級別與捕獲的示例代碼

    java異常級別與捕獲的示例代碼

    本次模擬一個異常實例,驗證一下異常的捕獲,通過實例代碼給大家解析java異常級別與捕獲的操作方法,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Java實現(xiàn)簡單抽獎功能界面

    Java實現(xiàn)簡單抽獎功能界面

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單抽獎功能界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別

    簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別

    這篇文章主要介紹了簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java設(shè)計模式之單例模式深入探索

    Java設(shè)計模式之單例模式深入探索

    單例模式(Singleton Pattern)是 Java 中最簡單的設(shè)計模式之一。這種類型的設(shè)計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,這種模式涉及到一個單一的類,該類負責創(chuàng)建自己的對象,同時確保只有單個對象被創(chuàng)建
    2021-10-10
  • 最新評論