I'm trying to call a method in Spring-powered bean asynchronously using @Async
. I defined an executor in XML:
<task:executor id="emailTasksExecutor" pool-size="1" />
and here is my method:
@Override
@Async("emailTasksExecutor")
public void sendEmail()
{
...
}
And the method does not get called at all when I use qualifier (emailTasksExecutor
). However, if I remove it, everything works ok. But in this case the default executor is used and I need to change this behaviour.
I thought the problem is that my class does not implement any interfaces, and something went wrong with proxies. But extracting the interface did not help.
To enable the asynchronous processing, add the @EnableAsync annotation to the configuration class. The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool.
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.
Never use @Async on top of a private method. In runtime, it will not able to create a proxy and, therefore, not work.
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.
So, the problem was my maven-aspectj-plugin
. I found the solution here. All I need to do is to add mode="aspectj"
to the task:annotation-driven
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With