0

As I found, controllers in String are singletones Are Spring MVC Controllers Singletons?

The question is, how Spring handles multiple long time consuming requests, to the same mapping? For example, when we want to return a model which require long calculations or connection to other server- and there are a lot of users which are sending request to the same url?

Async threads I assume- are not a solution, because method need to end before next request will be maintained? Or not..?

Community
  • 1
  • 1
Krystian
  • 2,221
  • 1
  • 26
  • 41
  • 2
    The fact that there is a single instance doesn't mean it cannot handle multiple requests at once... So no the method doesn't need to end before the next request can be handled. – M. Deinum Nov 08 '16 at 21:28

1 Answers1

0

Requests are handled using a thread-pool (Container-managed), so each request has an independent context, it does not matter whether if the Controller is Singleton or not.

One important thing is that Singleton instances MUST NOT share state between requests to avoid unexpected behaviours or race conditions.

The thread-pool capacity will define the number of requests the server could handle in a sync model.

If you want an async approach you coud use many options like:

  1. Having a independent thread pool that processes tasks from container threads, or
  2. Use a queue to push tasks and use an scheduler process tasks, or
  3. Use Websockets to make requests and use (1) or (2) for processing and then receive the notification when done.
Edwin Miguel
  • 409
  • 4
  • 11