Search code examples
javaspring-webfluxproject-reactorreactorspring5

WebFlux: how to work takeUntilOther() method?


Can someone please explain how the takeUntilOther() method works? I tried to run the following code, but it shows nothing on my console.

     Mono.just(10)
                .subscribe();

        Flux.range(1, 5)
                .takeUntilOther(Mono.just(10))
                .subscribe(System.out::println);

I don't understand why.


Solution

  • Kirill,

    I'd suggest you referring to the appropriate part of the project reactor's documentation.

    enter image description here

    takeUntilOther(Publisher<?> other) Relay values from this Flux until the given Publisher emits.

    Meaning, you will be receiving values from the original Flux until given Publisher<?> other starts producing events. In your case, you have a hot publisher just() that interrupts the original Flux immediately (by calling cancel() method).

    I will give you one more example. Have a look at the following code snippet:

    Flux.range(1, 5) // produces elements from 1 to 5
            .delayElements(Duration.ofSeconds(1)) // delays emission of each element from above for 1 second
            .takeUntilOther(Mono
                    .just(10) // hot publisher. emits one element
    
                    // delays '10' for 3 seconds. meaning that it will only 
                    // appears in the original Flux in 3 seconds
                    .delayElement(Duration.ofSeconds(3)) 
            )
            .subscribe(System.out::print);
    

    The output is:

    12