What happens when a non-blocking v/s blocking code is called in doOnNext()

127 Views Asked by At

I want to understand what happens when we execute a blocking v/s non-blocking code within a doOnNext block

fun test1(): Mono<ResponseFromTest2> {
        return test2()
            .flatMap { responseFromTest2 ->
               // do some operation
               Mono.just(responseFromTest2) 
             }
            .doOnNext {
                publishMetrics(value1)
                publishMetrics(value2)
            }
    }

in the above function, the controller calls test1 to eventually return some response. test1 calls test2, performs some operation and returns the Mono emitted from test2. During this flow, I need to publish two metrics. I wanted to use doOnNext to publish the metrics and continue the flow with the Mono from the flatMap.

I wanted to know how to avoid blocking code when calling publishMetrics(). If publishMetrics() is a void function, would this be a blocking code and less efficient? Would it be a better approach for publishMetrics() to return, for example, Mono<Boolean> to avoid blocking the thread. WRT to this, what's going to happen to the Mono emitted from publishMetrics() within .doOnNext? In general, what happens to the mono emitted by functions called within .doOnNext?

0

There are 0 best solutions below