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

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.

主站蜘蛛池模板: 卢龙县| 永清县| 贡嘎县| 阜新市| 吴旗县| 嵩明县| 醴陵市| 濮阳县| 汾阳市| 丹凤县| 宜章县| 视频| 柳林县| 通渭县| 乌鲁木齐市| 江安县| 榕江县| 同江市| 泾川县| 文登市| 芮城县| 奉新县| 佛冈县| 五原县| 清水县| 铜山县| 四会市| 天柱县| 乌海市| 赣州市| 潜江市| 竹山县| 绿春县| 察哈| 吴桥县| 巨鹿县| 天镇县| 任丘市| 洞口县| 平利县| 屯昌县|