官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 中牟县| 宜春市| 奈曼旗| 辛集市| 兴国县| 江津市| 丰台区| 土默特右旗| 松原市| 揭东县| 博兴县| 仁化县| 新竹县| 收藏| 如东县| 安义县| 武穴市| 雅江县| 攀枝花市| 丰宁| 瓦房店市| 阿巴嘎旗| 岱山县| 遵义县| 江油市| 巢湖市| 龙江县| 时尚| 隆子县| 隆子县| 青神县| 信阳市| 饶河县| 门源| 阳山县| 恩施市| 勃利县| 玛多县| 鄄城县| 尼勒克县| 庆阳市|