支持异步的JavaSpringMVC与SpringWebFlux
正如我所知(如果我错过了smth,请添加),当我们使用Spring MVC
应用程序时,我们有一个来自服务器的线程池(Tomcat…)当请求来自池中的一个线程处理此请求时,有时这是一个坏消息,因为如果任务花费很长时间,我们的线程将一直处于繁忙状态,为了避免这种行为,我们可以将控制器的返回类型从例如字符串改为Callable<String>
或DefferedResult<String>
,现在,来自tomcat的工作线程将执行交给来自ExecutorService(我们在@Configuration
类中配置)的线程,这更好,因为tomcat线程可以处理其他请求,并且不会等待每个请求的执行。
但我不明白WebFlux的想法。正如我从官方文档中了解到的,我们有一个线程来处理所有请求,然后(我不知道如何)返回响应,但是如果一个请求等待较长的任务(查询数据库),这是否意味着该线程将等待当前任务完成,或者它将为此任务创建新线程,如果是这样的话,它与Callable和DefferedResult有何不同
提前感谢
# 1 楼答案
本视频:
https://www.infoq.com/presentations/servlet-reactive-stack
解释如何在servlet和反应式堆栈中处理请求,以及反应式方法的好处
还有一个很好的解释,为什么RxJava(另一个反应性库)比期货更好:
http://reactivex.io/intro.html