gpt4 book ai didi

java - 如何为 Async Spring 使用多个 threadPoolExecutor

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:01 26 4
gpt4 key购买 nike

我在两个类上使用 Spring @Async。两者最终都实现了一个接口(interface)。我正在创建两个单独的 ThreadPoolTask​​Executor,因此每个类都有自己的 ThreadPool 来处理。然而,由于我认为代理和 Spring 如何实现异步类,我必须将 @Async 注释放在基本接口(interface)上。因此,这两个类最终使用相同的 ThreadPoolTask​​Executor。是否可以告诉 Spring 对于这个 Bean(在本例中我将实现该接口(interface)的类称为服务),使用这个 ThreadPoolTask​​Executor。

最佳答案

默认情况下,在方法上指定 @Async 时,将使用的执行器是提供给 'annotation-driven' 元素的执行器,如 here 所述。 .

但是,当需要指示在执行给定方法时应使用默认执行程序以外的执行程序时,可以使用 @Async 注释的 value 属性。

@Async("otherExecutor")
void doSomething(String s) {
// this will be executed asynchronously by "otherExecutor"
}

在这种情况下,“otherExecutor”可以是 Spring 容器中任何 Executor bean 的名称,或者可以是与任何 Executor 关联的限定符的名称,例如由元素或 Spring 的 @Qualifier 注释指定

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

并且您可能需要在您的应用程序中使用您希望的池设置指定 otherExecutor bean。

@Bean
public TaskExecutor otherExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);

return executor;
}

关于java - 如何为 Async Spring 使用多个 threadPoolExecutor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com