Where should I put @EnableAsync annotation
Asked Answered
P

3

20

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.

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

@Async
sendEmailQuote(listOfData){}

@Async
sendEmailWorkflowTaskAssignment(listOfData){}

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

I refer this sample

Pimp answered 6/4, 2017 at 2:25 Comment(0)
M
24

EnableAsync is used for configuration and enable Spring's asynchronous method execution capability, it should not be put on your Service or Component class, it should be put on your Configuration class like:

@Configuration
@EnableAsync
public class AppConfig {

}

Or with more configuration of your AsyncExecutor like:

@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 doc for more details.

And for the tutorial you followed, EnableAsync is put above Application class, which extends AsyncConfigurerSupport with AsyncExecutor configuration:

@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;
}
}
Mechanotherapy answered 6/4, 2017 at 2:56 Comment(2)
If you follow spring.io/guides/gs/async-method step by step, it will work. The correct config would be: 1. @Service is above your MyService class; 2. @Async is above your method in MyService , 3. @EnableAsync, also with @SpringBootApplication be above your Application class. Or could you please post all your code already tried?Mechanotherapy
what if I want use it in spring project,not springboot project?Drawplate
P
21

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

Peep answered 20/6, 2017 at 21:57 Comment(0)
C
0

Let's give light on some use case of @EnableAsync with @Async and Executor Framework.

You can use @EnableAsync in your any configuration (@Configuration) class as well as Your main class/Driver class of the Application(@SpringBootApplication).

@EnableAsync: It indicates spring boot to enables asynchronous behavior for all that methods which are annotated with @Async.

@Async: This annotation is generally used over the method, which contains the logic related to Asynchronous task, like Sending Email, OTP, Service Messages, Etc.. This methods are generally a part of any Service or class which is annotated with @Service or @Component annotation.

Executor Framework: Generally we are preferring to use Executor framework to maintain threads and its lifecycle(creation, task assignment, etc..)

You can create a Bean of ThreadPoolTaskExecutor and can set the required properties like, core pool size, max pool size, queue capacity for tasks, can set thread naming prefix for tracing, set rejection handler (Exception for for denying the entry of in queue). Check the below snippet.

@Override
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(2);
    executor.setMaxPoolSize(4); //Generally it should higher than core pool size
    executor.setQueueCapacity(400);
    executor.setThreadNamePrefix("Executor-Thread");
    executor.setRejectedExecutionHandler((r, exception)->sysout("Thread Queue is Full."));
    executor.initialize();
    return executor;
}

Hope this info will helps you.! Keep Learning.! Give up-vote if it helps...!

Colombo answered 12/2 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.