Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

reactive programming - How REST endpoints are auto subscribed while calling from Browser/REST Client?

In ProjectReactor or Reactive Streams, Nothing Happens Until You subscribe().

Reactive streams data flow will not happen unless until someone subscribe to it, but I see for all REST APIs like finds, save and inserts are not calling subscribe explicitly but data is flowing between producer and subscribers.

@RestController
class PersonController {

      private final PersonRepository repository;

      public PersonController(PersonRepository repository) {
        this.repository = repository;
      }
      @GetMapping("/all")
      public Flux<Person> index() {

         return repository.findAll();

     }
      @GetMapping("/people")
      Flux<String> namesByLastname(@RequestParam Mono<String> lastname) {

        Flux<Person> result = repository.findByLastname(lastname);
        return result.map(it -> it.getFullName());
      }

      @PostMapping("/people")
      Flux<People> AddPeople(@RequestBody Flux<Person> people) {

          return repository.saveAll(people);
      }
}

why do we no need to call subscribe for REST Endpoints to start a data flow in Project Reactor?

How REST endpoints (HTTP requests) are auto-subscribing to Reactive Streams for data flow when i call from browser?

am i missing something here ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're right - when your application is setting up a Flux/Mono reactive pipeline, nothing in that pipeline is executed until something subscribe to it.

Here's what's happening during a request/response exchange in Spring WebFlux:

  • the server receives a request and forwards that to WebFlux
  • depending on the request and your application code, a reactive pipeline will be built, involving filters, controllers, etc. You could see that as a pipe linking the request to the response
  • the HTTP client, through the TCP stack, requests reads and that backpressure information is transmitted by the underlying server.

The lowest contract in Spring WebFlux is HttpHandler - it's the contract that interfaces with the underlying server. In the case of Reactor Netty, this server already supports the reactive streams API and the subscription is natively done by the server. For other Servlet-based servers, we're using a reactive streams bridge to Servlet 3.1. In ServletHttpHandlerAdapter, we're bridging between the reactive streams world and the async I/O Servlet API - the subscription actually happens within that bridge.

Also: note that we don't usually subscribe to the value returned by WebClient; you can only do that if you're not in the middle of a reactive pipeline (i.e. not in the middle of a Controller handler). In those cases, we usually plug that into a reactive operator in the middle of the pipeline; if you don't, you'll have no guarantee whatsoever about when you'll get the HTTP client response - this totally decouples that call from the rest of your application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...