3

Is there a drawback to creating and managing your own ExecutorService vs using Spring Boot's @Async on a method with an @Bean method to create an Executor?

To me, doing it manually seems much simpler. I simply create my ExecutorService and a method that uses it to schedule a task/Callable.

I Spring Boot I would need:

  • Decorate the method with @Async
  • Decorate my Configuration class with @Configuration, @EnableAsync
  • Add an @Bean to create an Executor

Are there potential issues to doing it manually? Will Spring possibly kill the thread? Are there some other benefits to using the Spring Async pattern?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

1 Answers1

1

It would seem most of your hangups are items that are true for a lot of Spring IoC Configuration. The @Async is part of the Spring Framework not Spring Boot.

You could handle the executor yourself as you have mentioned but I would question why bother if you have already started to to use the Spring Framework and Spring Boot.

Spring does handle some items itself in the background, such as shutdown of the executor when closing the ApplicationContext.

There is also some nice baked in exception handling that can be used along with a complimenting the scheduling aspect of the Spring Framework if you choose to you use it.

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-qualification

Having to create @Bean, @Configuration is a fact of life with Spring and in my opinion not really a draw back, it's just a fact of life with the framework.

Finally, @Async abstracts the need to implement Runnable, or @Callable effectively allow faster implementations of asynchronous capabilities.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54