@Async(value = "")
可以使用 Executor
或 TaskExecutor
名称来运行异步进程。
我的目标是使用默认的 spring
实现执行一般的 @Async
方法,但要有一个特定的方法 @Async(value = "myExecutor")
与特定执行程序一起运行。
我想使用一次只运行一个线程的执行器,并将任何其他数据保存在队列中。
@Bean
public ExecutorService getMyTaskExecutor() {
return Executors.newFixedThreadPool(1);
}
问题:我现在如何将此 ExecutorService
应用到我的@Async
方法?
@Async(value = "myTaskExecutor")
public void myprocess() {
}
@Async(value = "asdasdasdMyTaskExecutor")
public void myprocess() {
}
问题是:无论我在这里写什么值,应用程序总是会启动。所以我不能确定这里应用了正确的执行者。我必须使用什么 bean 名称?
最佳答案
引自@Bean
文档:
While a
name
attribute is available, the default strategy for determining the name of a bean is to use the name of the@Bean
method.
因此,如果您的方法名为 getMyTaskExecutor
,您应该有 @Async(value = "getMyTaskExecutor")
,即执行器的名称必须对应于bean 的名称,默认情况下是 @Bean
注释方法的名称。
关于java - 如何将特定的 ExecutorService 与 @Async 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597914/