- Hands-On Reactive Programming in Spring 5
- Oleh Dokuka Igor Lozynskyi
- 179字
- 2021-07-23 16:36:20
Configuring asynchronous support
To run everything, we need an entry point for our application with the following customized methods:
@EnableAsync // (1)
@SpringBootApplication // (2)
public class Application implements AsyncConfigurer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public Executor getAsyncExecutor() { // (3)
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();// (4)
executor.setCorePoolSize(2);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(5); // (5)
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
return new SimpleAsyncUncaughtExceptionHandler(); // (6)
}
}
As we can see, the example is a Spring Boot application (2), with an asynchronous execution enabled by the @EnableAsync annotation (1). Here, we may configure an exception handler for exceptions thrown from the asynchronous execution (6). That is also where we prepare Executor for asynchronous processing. In our case, we use ThreadPoolTaskExecutor with two core threads that may be increased to up to one hundred threads. It is important to note that without a properly configured queue capacity (5), the thread pool is not able to grow. That is because the SynchronousQueue would be used instead, limiting concurrency.
推薦閱讀
- Mastering Node.js(Second Edition)
- Building E-commerce Sites with VirtueMart Cookbook
- EDA技術(shù)與VHDL編程
- Truffle Quick Start Guide
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 面向物聯(lián)網(wǎng)的嵌入式系統(tǒng)開發(fā):基于CC2530和STM32微處理器
- 面向云平臺(tái)的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- The Kubernetes Workshop
- Echo Quick Start Guide
- 網(wǎng)絡(luò)AI+:2030后的未來網(wǎng)絡(luò)
- 物聯(lián)網(wǎng)頂層設(shè)計(jì)與關(guān)鍵技術(shù)
- Building RESTful Web Services with .NET Core
- 工業(yè)以太網(wǎng)技術(shù):AFDX/TTE網(wǎng)絡(luò)原理、接口、互連與安全
- 物聯(lián)網(wǎng)M2M開發(fā)技術(shù):基于無線CPU-Q26XX