有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

支持异步的JavaSpringMVC与SpringWebFlux

正如我所知(如果我错过了smth,请添加),当我们使用Spring MVC应用程序时,我们有一个来自服务器的线程池(Tomcat…)当请求来自池中的一个线程处理此请求时,有时这是一个坏消息,因为如果任务花费很长时间,我们的线程将一直处于繁忙状态,为了避免这种行为,我们可以将控制器的返回类型从例如字符串改为Callable<String>DefferedResult<String>,现在,来自tomcat的工作线程将执行交给来自ExecutorService(我们在@Configuration类中配置)的线程,这更好,因为tomcat线程可以处理其他请求,并且不会等待每个请求的执行。 但我不明白WebFlux的想法。正如我从官方文档中了解到的,我们有一个线程来处理所有请求,然后(我不知道如何)返回响应,但是如果一个请求等待较长的任务(查询数据库),这是否意味着该线程将等待当前任务完成,或者它将为此任务创建新线程,如果是这样的话,它与Callable和DefferedResult有何不同

提前感谢


共 (1) 个答案

  1. # 1 楼答案

    本视频:

    https://www.infoq.com/presentations/servlet-reactive-stack

    解释如何在servlet和反应式堆栈中处理请求,以及反应式方法的好处

    还有一个很好的解释,为什么RxJava(另一个反应性库)比期货更好:

    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.