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

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.

主站蜘蛛池模板: 西丰县| 安阳县| 静乐县| 普安县| 大英县| 美姑县| 丹凤县| 塔河县| 东海县| 宿松县| 嵊州市| 仙居县| 密云县| 福海县| 华阴市| 洱源县| 辰溪县| 东丰县| 阿荣旗| 镇坪县| 阿坝县| 康定县| 海晏县| 平武县| 漠河县| 施秉县| 张家口市| 井陉县| 泗水县| 乐都县| 屯留县| 海晏县| 隆化县| 突泉县| 平潭县| 固始县| 冕宁县| 丰镇市| 林口县| 尚志市| 扎鲁特旗|