这是关于在使用 @Async 注解调用相同方法时如何使用不同的执行器。
问题是我们希望对某些外部调用有重试(和记录)策略。所有这些逻辑都在带有@Async注释的方法中,并且要执行的业务逻辑被传递给它(一个Runnable):
@Async(value = "DefaultTaskExecutor}")
public void executeAsyncAndRetry(AsyncTask asyncTask) {
...
}
但我们希望根据调用的不同,使用不同的线程池。
我认为这个 @Async 注释不允许 el 从参数或类似的东西中获取执行器。
是否有其他方法为此配置该注释,或者使用不同的方法来指定不同的执行器的不同方法?
最佳答案
是的,你可以。您需要在 spring 上下文文件中定义一个执行器 bean,并像这样在 @Async("executor id") 中传递该 bean id。
<bean id="otherExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
@Async("otherExecutor")
void doSomething(String s) {
// this will be executed asynchronously by "otherExecutor"
}
更多信息请引用:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
关于java - 如何在一个@Async中使用不同的执行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35743807/