Mono/Flux: how to suspend thread and wait for event or timeout

Andrey :

I would like to ask advice how to solve with Mono/Flux suspending and reactivating by delay or timeout.

The task is: application will receive HTTP request and should provide response.

When request is received using async sockets message should be sent. And we need to wait for specific answer, but no more than 30 seconds.

So I need to suspend thread until some Runnable will be called, or other option is each 0.2 sec query some variable and if it set to relevant value continue the process.

Could you please give me some suggestions?

Thank you

Andrey :

Finally I found the solution.

Maybe not most elegant but works, using recursion

This code querying variable state to get positive response, but not more than 10 seconds timeout.

val delayDuration = Duration.ofMillis(200)
val maximumAttempts = 50

fun createDelayedMono(counter : Int) : Mono<BigInteger> {

    val mono = Mono.delay(delayDuration).flatMap {
        it ->

        if (counter < maximumAttempts && reactorHelper.isEventCompleted(rrn)) {
            reactorHelper.removeEvent(rrn)
            return@flatMap Mono.just(BigInteger.ZERO)
        } else {
            return@flatMap createDelayedMono(counter + 1)
        }

    }

    return mono
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311304&siteId=1