Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Mutiny for CoroutineVerticle for awaitSuspending Support #229

Closed
murphye opened this issue Dec 28, 2022 · 1 comment
Closed

Comments

@murphye
Copy link

murphye commented Dec 28, 2022

It would be great to have Mutiny support in this project for CoroutineVerticle as Mutiny is now a preferred Reactive API and has support for Coroutines. This would allow mixing of Reactive APIs with Mutiny with the Coroutines via suspend fun.

Here is some working example code to make it possible to use the io.smallrye.mutiny.coroutines.awaitSuspending with the CoroutineVerticle.

import io.vertx.core.Context
import io.vertx.kotlin.coroutines.CoroutineVerticle
import io.vertx.kotlin.coroutines.dispatcher
import io.vertx.mutiny.core.Vertx
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.CoroutineContext

abstract class MutinyCoroutineVerticle : CoroutineVerticle() {

    protected lateinit var vertx: Vertx
    private lateinit var context: Context

    override val coroutineContext: CoroutineContext by lazy { context.dispatcher() + SupervisorJob() }

    override fun init(vertx: io.vertx.core.Vertx, context: Context) {
        this.vertx = Vertx(vertx)
        this.context = context
    }
}

Here is an example of using awaitSuspending().

import io.smallrye.mutiny.coroutines.awaitSuspending
import io.vertx.core.DeploymentOptions
import io.vertx.mutiny.core.Vertx
import kotlin.Array
import kotlin.String

suspend fun main(args: Array<String>) {
    val vertx = Vertx.vertx()
    println("Deployment Starting")
    vertx.deployVerticle({ MainVerticle() }, DeploymentOptions()).awaitSuspending()
    println("Deployment completed")
}

class MainVerticle : MutinyCoroutineVerticle() {
    private var counter = 0L

    override suspend fun start() {

        vertx.periodicStream(2000L)
            .toMulti()
            .subscribe().with { tick -> counter++ }

        vertx.createHttpServer()
                .requestHandler { req -> req.response().endAndForget("@$counter") }
                .listen(8080)
                .awaitSuspending()
   }
}
@murphye murphye changed the title Support for Mutiny Vert.x Conext for CoroutineVerticle to support awaitSuspending Support for Mutiny for CoroutineVerticle for awaitSuspending Support Dec 28, 2022
@tsegismont
Copy link
Contributor

Thanks @murphye for your proposal. I think it could be a nice addition to https://github.com/smallrye/smallrye-mutiny-vertx-bindings (as new module?) cc @jponge

@tsegismont tsegismont closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants