4

I am new to Java and working on an existing Spring MVC application. The project has a controller where many endponts are decorated with @Async annotation. But nowhere in the project did I find @EnableAsync annotation.I know @EnableAsync should be placed in one of the @Configuration classes or @SpringBootApplication classes. So I am not sure how the @Async is working of these existing endpoints

@RequestMapping(method= RequestMethod.POST, values= /endpoint1, consumes="application/json"
@Async
Public CompletableFuture<String> resultsForEndpoint1()
{
}

Question1- Can someone please explain if @Async can work without @EnableAsync

Question2- I need to implement a asynchronous method and perform a long running task on a separate thread and return response from an API without waiting for this long running task to complete. For this i will have to use @EnableAsync. Will Adding @EnableAsync to @SpringBootApplication affect other existing Apis which are decorated with @Async

Question3 - There are many classes decorated with @Configuration. Can I use any of these to decorate with @EnableAsync. Which one can I use since many classes have @Configuration

kimimishra
  • 51
  • 6

1 Answers1

1

Question 1 - @Async needs @EnableAsync but could be implicitly. Do you have a dependency to spring-boot-starter-actuator? According to this answer, it enables async and scheduling by default.

Question 2 - Adding @EnableAsync will effect all @Async. In fact, if you can prove that your existing application is already executing your @Async endpoint with a different thread, then all @Async are already working without adding @EnableAsync; as mentioned in Question 1.

Question 3 - Yes, you can use any of them or even create a new class if you think existing ones are irrelevant.

Wit
  • 605
  • 5
  • 17