我有一个 spring boot 应用程序,其中有两个我想要多任务处理的任务,我创建了两个 ThreadPoolTaskExecutor,每个任务一个,这是最好的方法吗?我只需要一个 ThreadPoolTaskExecutor 吗?以下是我的方法,我想知道哪种方法最好。
方法#1
@Bean("billTaskExecutor")
public ThreadPoolTaskExecutor billTaskExecutor() {
ThreadPoolTaskExecutor billTaskExecutor= new ThreadPoolTaskExecutor();
billTaskExecutor.setCorePoolSize(6);
billTaskExecutor.setMaxPoolSize(15);
billTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
billTaskExecutor.initialize();
return billTaskExecutor;
}
@Bean("rewardsTaskExecutor")
public ThreadPoolTaskExecutor rewardTaskExecutor() {
ThreadPoolTaskExecutor rewardsTaskExecutor= new ThreadPoolTaskExecutor();
rewardsTaskExecutor.setCorePoolSize(5);
rewardsTaskExecutor.setMaxPoolSize(10);
rewardsTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
rewardsTaskExecutor.initialize();
return billTaskExecutor;
}
@Async("billTaskExecutor")
public CompletableFuture<Bill> getClientBills(String clientName) {
return CompletableFuture.completedFuture(billProxy.getClientBills(clientName));
}
@Async("rewardsTaskExecutor")
public CompletableFuture<Reward> getClientRewards(String clientName) {
return CompletableFuture.completedFuture(RewardProxy.getClientBills(clientName));
}
方法#2
@Bean("customTaskExecutor")
public ThreadPoolTaskExecutor customTaskExecutor() {
ThreadPoolTaskExecutor customTaskExecutor= new ThreadPoolTaskExecutor();
customTaskExecutor.setCorePoolSize(5);
customTaskExecutor.setMaxPoolSize(10);
customTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
customTaskExecutor.initialize();
return customTaskExecutor;
}
@Async("customTaskExecutor")
public CompletableFuture<Bill> getClientBills(String clientName) {
return CompletableFuture.completedFuture(billProxy.getClientBills(clientName));
}
@Async("customTaskExecutor")
public CompletableFuture<Reward> getClientRewards(String clientName) {
return CompletableFuture.completedFuture(RewardProxy.getClientBills(clientName));
}
预先感谢您的建议。