Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Async does not work with task:executor

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.

like image 610
madhead - StandWithUkraine Avatar asked May 08 '14 08:05

madhead - StandWithUkraine


People also ask

How do you enable the use of @async annotation?

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.

What will happen if you specify @async over a public method of a Spring bean?

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.

Can we use @async on private method?

Never use @Async on top of a private method. In runtime, it will not able to create a proxy and, therefore, not work.

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

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.

like image 156
madhead - StandWithUkraine Avatar answered Sep 28 '22 06:09

madhead - StandWithUkraine