rx-javavert.x

rxSend vs send in Rxified API for Http Client in Vert.x


I am trying to understand a difference between below two snippets using rxRequest/rxSend vs request/send. They seem to behave in the same way.

RecordParser parser = RecordParser.newDelimited("\n", h -> log.info("r={}", h.toString()));
client
    .rxRequest(HttpMethod.GET, sut.actualPort(), "localhost", "/stream?file=stream2.txt")
    .flatMap(request -> request.rxSend().flatMap(HttpClientResponse::body))
    .subscribe(
        body -> {
          parser.handle(body.getDelegate());
          ctx.completeNow();
        },
        ctx::failNow);

// vs

RecordParser parser = RecordParser.newDelimited("\n", h -> log.info("r={}", h.toString()));
client
    .request(HttpMethod.GET, sut.actualPort(), "localhost", "/stream?file=stream2.txt")
    .flatMap(request -> request.send().flatMap(HttpClientResponse::body))
    .subscribe(
        body -> {
          parser.handle(body.getDelegate());
          ctx.completeNow();
        },
        ctx::failNow);

I understand that logic for rxRequest/rxSend is executed later at subscription time where request/send methods call the former methods and then subscribe immediately. What are practical examples when rxRequest/rxSend are needed and when not.


Solution

  • If you are building a chain of operations with RxJava, it is much better to use the Vert.x rxXXX methods, which will not perform any action until subscription happens.

    The simple methods can be useful for fire-and-forget operations. Also, developers new to reactive programming often forget to subscribe, and then don't understand why nothing happens. These methods can help in this case.