-
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.
perf($Starter): enable Spring Boot async task
- Loading branch information
1 parent
07dfbff
commit 418fc43
Showing
2 changed files
with
41 additions
and
3 deletions.
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
39 changes: 39 additions & 0 deletions
39
.../src/main/java/com/jmsoftware/maf/springbootstarter/configuration/AsyncConfiguration.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,39 @@ | ||
package com.jmsoftware.maf.springbootstarter.configuration; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.task.AsyncTaskExecutor; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
/** | ||
* Description: AsyncConfiguration, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/29/2021 4:39 PM | ||
**/ | ||
@Slf4j | ||
@EnableAsync | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AsyncConfiguration implements AsyncConfigurer { | ||
@Bean | ||
@Override | ||
public AsyncTaskExecutor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2); | ||
executor.setMaxPoolSize(100); | ||
executor.setBeanName("spring-boot-starter-thread-pool-task-executor"); | ||
// Specify the RejectedExecutionHandler to use for the ExecutorService. | ||
// Default is the ExecutorService's default abort policy. | ||
executor.setRejectedExecutionHandler((runnable, executor1) -> { | ||
log.error("Captured rejected execution. {}", runnable); | ||
// TODO: runnable persistence | ||
}); | ||
executor.initialize(); | ||
log.warn("Initial bean: '{}'", AsyncTaskExecutor.class.getSimpleName()); | ||
return executor; | ||
} | ||
} |