spring-bootexecutorservicerequestscopeprototype-scope

Implement runnable to serve a new request in springboot


I have a usecase in a spring boot application, where in we get a request, we send an acknowledgement back and then start a new executor task in background which will do some processing and send back some result.

Now I am having some doubts while creating the runnable task. I want for every request a new instance of this runnable task is submitted to the executor service. Could some clarify if keeping the scope as "prototype" should resolve my purpose or the scope should be "request". And if the latter is correct, is the default context in spring boot is web-aware?

Also I need to pass in some parameters in the runnable task. Any pointers would be appreciated for both the above problems.

TA


Solution

  • Spring can manage threads for you using the @Async annotation. This can be much simpler than managing them yourself if you are already using Spring.

    You can read about it here: https://www.baeldung.com/spring-async