October 26, 2024
Chicago 12, Melborne City, USA
java

How can I “block” after a `subscribe()`?


I’m trying to do some learning with Project Reactor in Java, and am getting a little hung up on using the subscribe() method on a Flux. Here’s a small example:

Flux.range(1, 3)
        .subscribeOn(Schedulers.boundedElastic())
        .subscribe(i -> System.out.println("Looking at: " + i));

System.out.println("All done!");

Which gives me the following output:

All done!
Looking at: 1
Looking at: 2
Looking at: 3

The behavior I’m looking to see would be to have the "All done!" message print after the "Looking at" messages. I can get there by removing the subscribeOn call, but I’m looking to have the method within the subscribe call executed asynchronously (obviously the println is contrived for this example). Is there a way that I could "block" until the Flux I’ve subscribed to has completed? Is there some way that I could go to a Mono<Void> or something?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video