DEV Community

Salad Lam
Salad Lam

Posted on

Project Reactor: Tips (1)

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay