一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot使用自定義配置的線程池執行Async異步任務

spring boot使用自定義配置的線程池執行Async異步任務

2021-03-23 13:24牛奮lch Java教程

這篇文章主要介紹了spring boot使用自定義配置的線程池執行Async異步任務,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

 

一、增加配置屬性類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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方法…………
}

 

二、創建線程池

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
 }
}

 

三、在主類中開啟配置支持

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
 }
}

 

四、測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.");
 }
}

 

五、測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.");
 }
}

測試結果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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類,示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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即可。

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

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/64132520

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 奇米白色| 爽好大快深点一视频 | 欧美成人免费观看国产 | 午夜福利体检 | 国内精品久久久久影院男同志 | 亚欧综合 | 色帽子影院| 成人啪精品视频免费网站 | 97蝌蚪自拍自窝 | 午夜精品久视频在线观看 | 息与子中文字幕在线 | 黑人巨鞭大战白妞10级 | 免费看国产一级特黄aa大片 | 动漫美女日批 | heyzo1754北岛玲在线视频 | 欧美高清milf在线播放 | 含羞草国产亚洲精品岁国产精品 | 亚州精品永久观看视频 | 亚洲爱v | 欧美大奶艳星 | 特黄特a级特别特级特毛片 特黄a级三级三级野战 | 国产99久久精品一区二区 | 欧美一区二区三区免费高 | 亚洲大片在线观看 | 亚洲免费福利视频 | 日本人成在线视频免费播放 | 啪啪免费入口网站 | 日韩理论片在线看免费观看 | 日本欧美强乱视频在线 | 三体动漫在线观看免费完整版2022 | 99在线免费观看 | 国产99视频精品免视看7 | 国产欧美曰韩一区二区三区 | 黑人又大又硬又粗再深一点 | 国产精品久久久久久久久免费hd | 免费高清在线观看 | 亚洲午夜精品久久久久久成年 | 国模娜娜a4u1546全套 | 男模chinesegayxxxx | 国产欧美一区视频在线观看 | 99er视频|