Project Reactor: Tips (1)

Salad Lam - Apr 13 - - Dev Community

Don't know to use which operators

In Reactor 3 Reference Guide, there is some use case may to use directly.

To check if subscriber is attached

Nothing is printed after executed following statement.

Flux<Integer> f = Flux.just(1, 2, 3, 4).map(v -> {
    LOGGER.debug("value received in map(): {}", v);
    return v * v;
});
Enter fullscreen mode Exit fullscreen mode

Remember to attach a subscriber, or blocking function at the end of the operator chain, otherwise nothing happened. Following is the possible function

  • subscribe()
  • blockFirst() (blocking)
  • blockLast() (blocking)
  • toIterable() (blocking)
  • toStream() (blocking)

About time of class instantiation of just()

Flux<Integer> f = Flux.just(new Car("Mercedes-Benz"), new Car("Toyoto"));   // (1)
f.subscribe();  // (2)
Enter fullscreen mode Exit fullscreen mode

Class Car will be instantiated when executing (1). If want to defer the class instantiation on (2), please wrap just() by defer()/deferContextual().

Flux<Integer> f = Flux.defer(() -> Flux.just(new Car("Mercedes-Benz"), new Car("Toyoto")));   // (1)
f.subscribe();  // (2)
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player