If I want to conflate an action happening one at a...
# coroutines
k
If I want to conflate an action happening one at a time but not blocking my request in Quarkus or any other Kotlin server framework is that the idiomatic Kotlin coroutines way?
Copy code
private val coroutineScope = CoroutineScope(SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>)

    private val channel = Channel<Int>(1)

    @POST
    @Path("/channel")
    suspend fun sendToChannel() {
        coroutineScope.launch {
            delay(Random.nextLong(1000, 10000))
            println("Send to Channel Start")
            channel.send(100)
            println("Send to Channel Finish")

            println("/channel finish")
        }
    }

    fun onStart(@Observes startupEvent: StartupEvent) {
        coroutineScope.launch {
            channel.consumeEach {
                println("Consume each: $it")
            }
        }
    }
s
• Your
sendToChannel
function shouldn't be
suspend
if all it's doing is launching a separate coroutine. • What do you mean by "conflate"? At the moment, your channel will deliver every item. You can change that by changing its capacity, e.g. to
Channel.CONFLATED
.
k
suspend
was only used for Quarkus to continue the request in
vert.x-eventloop-thread
and not unnecessarily switch to a worker thread. This avoids the thread switching even if the
suspend
is not used directly • I meant if consumptions happens sequentially for every event in the correct order. But yes that should be the case I tested it with the sample code and it should work