Spring 具有异步支持的 Mvc 与 Spring WebFlux
Spring Mvc with async support vs Spring WebFlux
据我所知(如果我错过了 smth,请添加)当我们使用 Spring MVC
应用程序时,我们有一个来自我们服务器的线程池(Tomcat...)当请求来自我们的一个时来自池的线程处理这个请求,有时这很糟糕,因为如果任务花费很长时间,我们的线程将一直很忙,为了避免这种行为,我们可以将控制器的 return 类型从 for Example String 更改为 Callable<String>
或 DefferedResult<String>
,现在来自 tomcat 的工作线程将执行权交给来自 ExecutorService 的线程(我们在 @Configuration
class 中配置),它更好,因为 tomcat 线程可以处理其他请求,不会等待每个请求的执行。
但我不明白 WebFlux 的想法。正如我从官方文档中了解到的那样,我们有一个线程来处理所有请求,然后(我不明白如何)return 响应,但是如果一个请求等待长任务(查询数据库)怎么办这意味着该线程将等待直到当前任务完成,或者它将为此任务创建新线程,如果是这样,它与 Callable 和 DefferedResult 有何不同?
提前致谢
这个视频:
https://www.infoq.com/presentations/servlet-reactive-stack
解释如何在 servlet 和反应式堆栈中处理请求以及反应式方法的好处。
也很好地解释了为什么 RxJava(另一个响应式库)比 Futures 更好:
http://reactivex.io/intro.html
Techniques like Java Futures are straightforward to use for a single
level of asynchronous execution but they start to add non-trivial
complexity when they’re nested.
It is difficult to use Futures to optimally compose conditional
asynchronous execution flows (or impossible, since latencies of each
request vary at runtime). This can be done, of course, but it quickly
becomes complicated (and thus error-prone) or it prematurely blocks on
Future.get(), which eliminates the benefit of asynchronous execution.
ReactiveX Observables, on the other hand, are intended for composing
flows and sequences of asynchronous data.
据我所知(如果我错过了 smth,请添加)当我们使用 Spring MVC
应用程序时,我们有一个来自我们服务器的线程池(Tomcat...)当请求来自我们的一个时来自池的线程处理这个请求,有时这很糟糕,因为如果任务花费很长时间,我们的线程将一直很忙,为了避免这种行为,我们可以将控制器的 return 类型从 for Example String 更改为 Callable<String>
或 DefferedResult<String>
,现在来自 tomcat 的工作线程将执行权交给来自 ExecutorService 的线程(我们在 @Configuration
class 中配置),它更好,因为 tomcat 线程可以处理其他请求,不会等待每个请求的执行。
但我不明白 WebFlux 的想法。正如我从官方文档中了解到的那样,我们有一个线程来处理所有请求,然后(我不明白如何)return 响应,但是如果一个请求等待长任务(查询数据库)怎么办这意味着该线程将等待直到当前任务完成,或者它将为此任务创建新线程,如果是这样,它与 Callable 和 DefferedResult 有何不同?
提前致谢
这个视频:
https://www.infoq.com/presentations/servlet-reactive-stack
解释如何在 servlet 和反应式堆栈中处理请求以及反应式方法的好处。
也很好地解释了为什么 RxJava(另一个响应式库)比 Futures 更好:
http://reactivex.io/intro.html
Techniques like Java Futures are straightforward to use for a single level of asynchronous execution but they start to add non-trivial complexity when they’re nested.
It is difficult to use Futures to optimally compose conditional asynchronous execution flows (or impossible, since latencies of each request vary at runtime). This can be done, of course, but it quickly becomes complicated (and thus error-prone) or it prematurely blocks on Future.get(), which eliminates the benefit of asynchronous execution.
ReactiveX Observables, on the other hand, are intended for composing flows and sequences of asynchronous data.