I am new to spring boot and trying an interesting scenario. I want to return the result of an api and post that do some kafka and rabbitMQ publishing and I don't want my api response to halt till the time I am doing publishing. I am trying to use @EnableAsync to enable async processing in the application and @async to define my methods.
@Async
fun publishOfferEventsAsync() {
Thread.sleep(10000)
// publish to rmq
logger().debug { "xxxyyyzzz" }
}
Can I directly call this async execution from other non-async methods of the bean? Is there a better way you can suggest. Thank you!
If both sync. and
@Asyncare defined in different objects , it is okay.If they are defined in the same object , it is not okay simply because spring AOP which
@Asyncis based on does not support self-invocation. (Refer this for details)If they really need to be defined in the same object , you can define another generic
@Asyncexecutor bean and call the async. method via it. Something likes :And then inject it to use it :