0

i want konw r2dbc principle, so i debug the code, but i find it work weird.

        Flux.range(1, 10)
                .flatMap(id -> repository.save(new Student(id, "lxp")))
                .doOnNext(System.out::println)
                .subscribe();

why it run all save before run doOnNext? In my understanding, it run save then doOnNext one by one.

it hard.hope someone can help me.thanks.

coolxp
  • 3
  • 1

1 Answers1

0

Answer is partially here

https://stackoverflow.com/a/57208219/182393

In addition, flatMap is emitting events (saves) in an asynchronous way. So, you have range of events, that are handled in flatMap asynchronously (all at the same time) and then they go to doOnNext, and finally to subscribe.

Your code is not sequential (and this is why reactive programming causes madness).

Koziołek
  • 2,791
  • 1
  • 28
  • 48
  • so do you know how to measure the time spent on individual elements? like the time taken by the number 1. I try to use Flux#tap,but i think it is asynchronous way. – coolxp Aug 18 '23 at 08:51
  • Personally, I will extract this lambda to another method and add some aspect around just to log time. Messy, but easy (if you use Spring or know how to use AspectJ). – Koziołek Aug 27 '23 at 19:24