- 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.
推薦閱讀
- 社交網(wǎng)絡對齊
- Spring Boot 2.0 Projects
- Hands-On Chatbots and Conversational UI Development
- Learning Karaf Cellar
- Proxmox High Availability
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 正在爆發(fā)的互聯(lián)網(wǎng)革命
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡實用技術(shù)速成
- 數(shù)字通信同步技術(shù)的MATLAB與FPGA實現(xiàn):Altera/Verilog版(第2版)
- 城市治理一網(wǎng)統(tǒng)管
- 設備監(jiān)控技術(shù)詳解
- 現(xiàn)代通信系統(tǒng)(第5版)
- 數(shù)字王國里的虛擬人:技術(shù)、商業(yè)與法律解讀
- 物聯(lián)網(wǎng),So Easy!
- 一本書讀懂移動物聯(lián)網(wǎng)