简体   繁体   English

Spring MVC控制器如何处理多个长HTTP请求?

[英]How Spring MVC controller handle multiple long http requests?

As I found, controllers in String are singletones Are Spring MVC Controllers Singletons? 正如我发现的那样,String中的控制器是单调的Spring MVC控制器是单调的吗?

The question is, how Spring handles multiple long time consuming requests, to the same mapping? 问题是,Spring如何处理多个耗时的请求到同一映射? 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. 使用线程池(由容器管理)处理请求,因此每个请求都具有独立的上下文,控制器是否为Singleton都无关紧要。

One important thing is that Singleton instances MUST NOT share state between requests to avoid unexpected behaviours or race conditions. 重要的一点是,Singleton实例不得在请求之间共享状态,以免发生意外行为或竞争状况。

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. 使用Websocket发出请求,并使用(1)或(2)进行处理,然后在完成时接收通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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