0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.