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

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.

主站蜘蛛池模板: 太白县| 北安市| 杨浦区| 高州市| 巴彦县| 苍梧县| 涞源县| 伊川县| 西乡县| 万安县| 道真| 如东县| 扎囊县| 光泽县| 苏州市| 绥宁县| 白银市| 新干县| 本溪| 海城市| 兴国县| 鹿邑县| 镇远县| 科尔| 沿河| 忻州市| 长垣县| 甘肃省| 施秉县| 枝江市| 十堰市| 鸡东县| 上饶市| 鲜城| 汶上县| 新和县| 平南县| 台安县| 海丰县| 丹阳市| 九龙县|