kam*_*aci 5 java asynchronous threadpool spring-boot
我有一个 Spring Boot 应用程序,它负责通过 REST 回答请求。我还推送有关我的应用程序调用的指标。由于这是一项单独的任务,我必须立即响应用户,因此我希望异步发布该指标。所以我用过:
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("MyApp-");
executor.initialize();
return executor;
Run Code Online (Sandbox Code Playgroud)
但是,这个使用SimpleAsyncTaskExecutor并且不重用任何线程。
1)我该如何使用ConcurrentTaskExecutor而不是SimpleAsyncTaskExecutor?
2)哪种实现最适合我的需求?
ygl*_*odt 10
要使您的自定义 Executor 工作,请确保将其注册为 Bean,并在用 注释的方法上引用它@Async:
@Bean(name = "transcodingPoolTaskExecutor")
public Executor transcodingPoolTaskExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("transcoder-");
executor.initialize();
return executor;
}
Run Code Online (Sandbox Code Playgroud)
并在其中@Service持有该方法:
@Async("transcodingPoolTaskExecutor")
public void transcodeVideo(UUID fileId) {
mediaFileService.transcodeVideo(fileId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1868 次 |
| 最近记录: |