简体   繁体   中英

How Spring MVC controller handle multiple long http requests?

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..?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM