Kir*_*rit 6 java spring asynchronous spring-boot
我在两个类上使用Spring @Async.两者都最终实现了一个接口.我正在创建两个单独的ThreadPoolTaskExecutor,因此每个类都有自己的ThreadPool来处理.但是由于我认为使用代理以及Spring如何实现异步类,我必须将@Async注释放在基接口上.因此,两个类最终都使用相同的ThreadPoolTaskExecutor.是否有可能告诉Spring对于这个Bean(在这种情况下我调用实现该接口的类的一个Service),使用这个ThreadPoolTaskExecutor.
Imr*_*ran 10
指定时默认@Async上的方法,该方法将被使用的执行程序是如所描述的提供给"注解驱动"元素的一个这里.
但是,@Async当需要指示在执行给定方法时应该使用除默认值之外的执行程序时,可以使用注释的value属性.
@Async("otherExecutor")
void doSomething(String s) {
// this will be executed asynchronously by "otherExecutor"
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,"otherExecutor"可以是Spring容器中任何Executor bean的名称,也可以是与任何Executor关联的限定符的名称,例如,使用元素或Spring的@Qualifier注释指定的名称
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
并且您可能需要使用所需的池设置在app中指定otherExecutor bean.
@Bean
public TaskExecutor otherExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);
return executor;
}
Run Code Online (Sandbox Code Playgroud)