java - 如何将特定的 ExecutorService 与 @Async 一起使用?

标签 java spring asynchronous

@Async(value = "") 可以使用 ExecutorTaskExecutor 名称来运行异步进程。

我的目标是使用默认的 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/

相关文章:

java - 如果 RadioButton 已被选中,则防止再次单击它

java - 如何使用 Hibernate\Spring Data 将 json 转换为 java 对象数据并保存到数据库中?

ios - Swiftdispatch_async 在 iOS 通知之前不会更新 UIView

java - spring TaskExecutor 类中的 Autowiring 对象为空

javascript - 如何杀死一个javascript函数?

java - Java 中 JSON 字符串到 JSON

java - java.io.PrintStream 上的线程争用

java - Spring Boot 使用调度程序关闭

java - 如何在 Spring Boot 2 中保护具有角色的执行器端点?

java - tomcat 8.0.15/jasper/tiles 3.0.5 的 NoClassDefFoundError