简体   繁体   中英

What is Spring MVC Based on Reactor?

I have just been reading everything I can about Spring and Reactor and do realize that Reactor is supposed to be included in the upcoming Spring Framework 5 (anyone using this in production btw?)

My interest is to use it in Spring MVC, since it is not currently part of the framework, how can Reactor be used in Spring MVC? It appears from online examples to use Reactor in Spring now while waiting for Framework 5, is to use reactor-bus.

Is Spring MVC + Reactor in its current state just adding the reactor-bus to a MVC app ?

A look at the Github shows that reactor-bus seems to be in legacy mode ?

What is the current way to give reactive capabilities to the existing Spring MVC ?

Spring 5 and WebFlux will give you the most benefit, because the framework itself is using Reactive Programming and is fully non-blocking, with opportunities for end-to-end asynchronicity if your DB is also async-capable (think Cassandra, Redis, MongoDB, Couchbase, along with the reactive Spring Data Kay).

That said, a library like Reactor can have benefits even if your app is not fully reactive. For example, if you have a service layer with a lot of orchestration needed. If these services represent tasks as asynchronous types (ideally Flux / Mono / Publisher , but also Future ), you can bridge them to Reactor and use the powerful operators to build up complex asynchronous processing pipelines.

The last piece is to let Spring 4.x work with these asynchronous results. The framework has a form of support for that in the DeferredResult<T> type, which you could obtain from a Flux or Mono (the example below is simplistic and doesn't show that composition of operators that I mentioned above, which would be hidden in the service):

@GetMapping()
public DeferredResult<User> getCurrentUser() {
    DeferredResult<User> result = new DeferredResult();
    Mono<User> mono = myService.getCurrentUser();
    mono.subscribe(
      value -> result.setResult(value),
      error -> result.setErrorResult(error)
    );
    return result;
}

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