-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add taskExecutorConf, add async annotation, add code comment
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
springWarehouse/src/main/java/com/yen/springWarehouse/config/TaskExecutorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.yen.springWarehouse.config; | ||
|
||
// https://github.com/timebusker/spring-boot/blob/master/spring-boot-5-Async/src/main/java/cn/timebusker/config/TaskExecutorConfig.java | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class TaskExecutorConfig implements AsyncConfigurer{ | ||
|
||
/** | ||
* Set the ThreadPoolExecutor's core pool size. | ||
*/ | ||
private static final int CORE_POOL_SIZE = 5; | ||
|
||
/** | ||
* Set the ThreadPoolExecutor's maximum pool size. | ||
*/ | ||
private static final int MAX_POOL_SIZE = 20; | ||
|
||
/** | ||
* Set the capacity for the ThreadPoolExecutor's BlockingQueue. | ||
*/ | ||
private static final int QUEUE_CAPACITY = 10; | ||
|
||
/** | ||
* 通过重写getAsyncExecutor方法,制定默认的任务执行由该方法产生 | ||
* | ||
* 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor | ||
* 这样我们就获得了一个基于线程池的TaskExecutor | ||
*/ | ||
@Override | ||
public Executor getAsyncExecutor() { | ||
|
||
System.out.println(">>> getAsyncExecutor ..."); | ||
|
||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); | ||
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);// 线程池维护线程的最少数量 | ||
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);// 线程池维护线程的最大数量 | ||
taskExecutor.setQueueCapacity(QUEUE_CAPACITY);// 线程池所使用的缓冲队列 | ||
taskExecutor.initialize(); | ||
|
||
return taskExecutor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return null; | ||
} | ||
|
||
/** | ||
* 自定义任务执行器:在定义了多个任务执行器的情况下,可以使用@Async("getMineAsync")来设定 | ||
* | ||
* @return | ||
*/ | ||
@Bean | ||
public Executor getMineAsync() { | ||
|
||
System.out.println(">>> getMineAsync ... "); | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(CORE_POOL_SIZE - 4); | ||
executor.setMaxPoolSize(MAX_POOL_SIZE - 10); | ||
executor.setQueueCapacity(QUEUE_CAPACITY - 5); | ||
executor.setThreadNamePrefix("mineAsync-"); | ||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务 | ||
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行 | ||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters