java 我应该把@EnableAsync 注释放在哪里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43244618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:13:53  来源:igfitidea点击:

Where should I put @EnableAsync annotation

javaspringasynchronousblazeds

提问by Sanka

I need to send a email in async way while saving the data into DB.

我需要以异步方式发送电子邮件,同时将数据保存到数据库中。

My approach was like this.

我的方法是这样的。

//I have tried with service layer annotating.But not worked. 
@EnableAsync 
class MyService{
 public String saveMethod(List listOfData){
    mail.sendEmailQuote(listOfData);
    mail.sendEmailWorkflowTaskAssignment(listOfData);
    myDao.saveData(listOfData);
 }
}

I need to perform following methods in @Async way. Where should I put @EnableAsync annotation. This is not a Schedule related thing. This is happen when user click save button. Application is used flex spring blazeDS. There is no controller written by my self.

我需要以@Async 方式执行以下方法。我应该把@EnableAsync 注释放在哪里。这不是与计划相关的事情。当用户单击保存按钮时会发生这种情况。应用程序使用弹性弹簧 blazeDS。没有我自己写的控制器。

I have used @Async annotation in my code for following 2 methods. Those are in class call Mail.

我在我的代码中使用了 @Async 注释来执行以下 2 种方法。那些是在课堂上调用邮件。

@Async
sendEmailQuote(listOfData){}

@Async
sendEmailWorkflowTaskAssignment(listOfData){}

Could you help me to find where should I put @EnableAsync ?

你能帮我找到我应该把 @EnableAsync 放在哪里吗?

I refer this sample

我参考这个样本

回答by shizhz

EnableAsyncis used for configuration and enable Spring's asynchronous method execution capability, it should not be put on your Serviceor Componentclass, it should be put on your Configurationclass like:

EnableAsync用于配置和启用 Spring 的异步方法执行能力,它不应该放在你的ServiceComponent类上,它应该放在你的Configuration类上,如:

@Configuration
@EnableAsync
public class AppConfig {

}

Or with more configuration of your AsyncExecutorlike:

或者有更多你AsyncExecutor喜欢的配置:

@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {

 @Override
 public Executor getAsyncExecutor() {
     ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
     executor.setCorePoolSize(7);
     executor.setMaxPoolSize(42);
     executor.setQueueCapacity(11);
     executor.setThreadNamePrefix("MyExecutor-");
     executor.initialize();
     return executor;
 }
 }

Please refer to it's java docfor more details.

有关更多详细信息,请参阅它的 java 文档

And for the tutorial you followed, EnableAsyncis put above Applicationclass, which extends AsyncConfigurerSupportwith AsyncExecutor configuration:

对于您遵循的教程,EnableAsync放在具有 AsyncExecutor 配置的Applicationextends AsyncConfigurerSupport之上:

@SpringBootApplication
@EnableAsync
public class Application extends AsyncConfigurerSupport {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(2);
    executor.setMaxPoolSize(2);
    executor.setQueueCapacity(500);
    executor.setThreadNamePrefix("GithubLookup-");
    executor.initialize();
    return executor;
}
}

回答by watts

Just make sure that @Async methods aren't called by the same class. Self invocation for proxy won't work.

只需确保@Async 方法不是由同一类调用即可。代理的自调用将不起作用。