Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put @EnableAsync annotation

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

like image 777
Sanka Avatar asked Apr 06 '17 02:04

Sanka


People also ask

What is the use of @EnableAsync annotation?

Annotation Type EnableAsync. Enables Spring's asynchronous method execution capability, similar to functionality found in Spring's <task:*> XML namespace.

What is @EnableAsync in Spring boot?

The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool. This class also customizes the Executor by defining a new bean. Here, the method is named taskExecutor , since this is the specific method name for which Spring searches.

What does @async annotation do?

Simply put, annotating a method of a bean with @Async will make it execute in a separate thread. In other words, the caller will not wait for the completion of the called method. One interesting aspect in Spring is that the event support in the framework also has support for async processing if necessary.

What is the use of @async in Spring boot?

Here @EnableAsync is used for enabling asynchronous processing with Java Spring Boot Configuration and switches Spring's ability to run @Async methods. The @Async Methods run in the background thread pool without interruption other parallel processes.


1 Answers

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;
}
}
like image 162
shizhz Avatar answered Oct 01 '22 04:10

shizhz