I wanted to make a method writing to DB as async using @Async annotation.
I marked the class with the annotation @EnableAsync:
@EnableAsync
public class FacialRecognitionAsyncImpl {
    @Async
    public void populateDataInPushQueue(int mediaId, int studentId) {
        //myCode
    }
}
while calling the populateDataInPushQueue method, the write operation should be executed in another thread and the flow should continue from the class I am calling this method. But this is not happening and the program execution is waiting for this method to complete.
In short, if your async method is an event handler or a callback, it's ok to return void .
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.
Never use @Async on top of a private method. In runtime, it will not able to create a proxy and, therefore, not work.
Task return typeSuch methods return void if they run synchronously. If you use a Task return type for an async method, a calling method can use an await operator to suspend the caller's completion until the called async method has finished.
The @Async annotation has few limitations - check whether those are respected:
public methods onlyvoid or Future
The following can be found at the documentation of @EnableAsync:
Please note that proxy mode allows for the interception of calls through the proxy only; local calls within the same class cannot get intercepted that way.
Another fact is that the class annotated with @EnableAsync must be a @Configuration as well. Therefore start with an empty class:
@EnableAsync
@Configuration
public class AsyncConfiguration { }
                        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