I'm experimenting with VertX+Couroutines and just want to check if this setup is blocking at any point or has potential issues that i need to be aware of.
For example, is runBlocking being used correctly in this instance or should i rather do a deployVerticle? And then inside requestHandler, i'm doing GlobalScope.launch, this seems to be discouraged, what is the correct scope to use here?
I've added VertX 4.0.0-milestone5 to my Gradle build script, i'm not using VertX Web:
val vertxVersion = "4.0.0-milestone5"
implementation("io.vertx:vertx-core:$vertxVersion") {
exclude(group = "com.fasterxml.jackson.core", module = "jackson-core")
exclude(group = "com.fasterxml.jackson.core", module = "jackson-databind")
exclude(group = "log4j", module = "log4j")
exclude(group = "org.apache.logging.log4j", module = "log4j-api")
exclude(group = "org.apache.logging.log4j", module = "log4j-core")
}
implementation("io.vertx:vertx-lang-kotlin:$vertxVersion")
implementation("io.vertx:vertx-lang-kotlin-coroutines:$vertxVersion")
Inside Routing.kt i have the following setup:
class Routing(
private val port: Int
) : CoroutineVerticle() {
override suspend fun start() {
Vertx.vertx().createHttpServer(
HttpServerOptions().setCompressionSupported(true)
).requestHandler { req ->
GlobalScope.launch {
try {
log.info("${req.method()}:${req.path()}")
req.response().setStatusCode(200).end("Hello World")
} catch (e: Exception) {
log.error(e.message ?: "", e)
req.response().setStatusCode(500).end("Something Went Wrong")
}
}
}.listen(port)
log.info("Listening on $port")
}
override suspend fun stop() {
}
companion object {
private val log = LoggerFactory.getLogger(Routing::class.java)
private val root = RoutingTree()
suspend fun setup(port: Int) {
Endpoint.all.forEach {
root.addPath(it.key, it.value)
}
log.info("\n" + root.toString())
Routing(port = port).start()
}
}
}
This Routing.setup is then used inside main()
object Server {
private val log = LoggerFactory.getLogger(this.javaClass)
#JvmStatic
#ExperimentalTime
fun main(args: Array<String>) = runBlocking {
....
// setup routing
Routing.setup(
port = if (ENV.env == LOCAL) {
5555
} else {
80
},
)
The whole point of Kotlin integration with Vert.x is that you don't have to use GlobalScope.launch
Here's a minimal example of how it can be achieved:
fun main() {
val vertx = Vertx.vertx()
vertx.deployVerticle("Server")
}
class Server : CoroutineVerticle() {
override suspend fun start() {
vertx.createHttpServer().requestHandler { req ->
// You already have access to all coroutine generators
launch {
// In this scope you can use suspending functions
delay(1000)
req.response().end("Done!")
}
}.listen(8888)
}
}
Related
i'm trying to create a component that stream data from remote service continuously. The component starts and stops according to spring container lifecycle. I'm not sure how to test this component as the subscription is done inside my component so i was wondering wether this is the correct way to implement this kind of component with webflux or not. Does anybody know any similar component in any framework from where i might take some ideas?
Regards
class StreamingTaskAdapter(
private val streamEventsUseCase: StreamEventsUseCase,
private val subscriptionProperties: subscriptionProperties,
) : SmartLifecycle, DisposableBean, BeanNameAware {
private lateinit var disposable: Disposable
private var running: Boolean = false
private var beanName: String = "StreamingTaskAdapter"
private val logger = KotlinLogging.logger {}
override fun start() {
logger.info { "Starting container with name $beanName" }
running = true
doStart()
}
private fun doStart() {
disposable = Mono.just(
CreateSubscriptionCommand(
subscriptionProperties.events,
subscriptionProperties.owningApplication
)
)
.flatMap(streamEventsUseCase::createSubscription)
.flatMap { subscription ->
Mono.just(subscription)
.map(::ConsumeSubscriptionCommand)
.flatMap(streamEventsUseCase::consumeSubscription)
}
.repeat()
.retryWhen(Retry.backoff(MAX_ATTEMPTS, Duration.ofSeconds(2)).jitter(0.75))
.doOnSubscribe { logger.info { "Started event streaming" } }
.doOnTerminate { logger.info { "Stopped event streaming" } }
.subscribe()
}
override fun stop() {
logger.info("Stopping container with name $beanName")
doStop()
}
override fun isRunning(): Boolean = running
private fun doStop() {
running = false
disposable.dispose()
}
override fun destroy() {
logger.info("Destroying container with name $beanName")
doStop()
}
override fun setBeanName(name: String) {
this.beanName = name
}
companion object {
const val MAX_ATTEMPTS: Long = 3
}
}
I have the following code, that it going to start a http server:
class MainVerticle : CoroutineVerticle() {
override suspend fun start() {
val server = vertx.createHttpServer()
val router = Router.router(vertx)
router.route("/api/genders*")
.subRouter(GenderApi(vertx).create())
server.requestHandler(router)
.listen(8080)
.await()
}
}
Now, I would like to output, if the server has been successfully started or failed(in case the port has been already occupied).
Without the Coroutine, the codes would be:
class MainVerticle : AbstractVerticle() {
override fun start(startPromise: Promise<Void>) {
val server = vertx.createHttpServer()
val router = Router.router(vertx)
server.requestHandler(router).listen(8888) { http ->
if (http.succeeded()) {
startPromise.complete()
println("HTTP server started on port 8888")
} else {
println(http.cause())
startPromise.fail(http.cause());
}
}
}
}
Here I do output, if the server has been started success or not.
If you use coroutines, add a try/catch block:
class MainVerticle : CoroutineVerticle() {
override suspend fun start() {
val server = vertx.createHttpServer()
val router = Router.router(vertx)
router.route("/api/genders*")
.subRouter(GenderApi(vertx).create())
try {
server.requestHandler(router)
.listen(8080)
.await()
println("HTTP server started on port 8888")
} catch (e: Exception) {
println(http.cause())
throw e
}
}
}
I am trying to listen to my ViewModels MutableStateFlow from my FlutterSceneView. But I get the following error when trying to set the listener from the views init:
Suspend function 'listenToBackgroundColor' should be called only from a coroutine or another suspend function
class FlutterSceneView(context: Context, private val viewModel: FlutterSceneViewModelType): PlatformView {
private val context = context
private val sceneView = SceneView(context)
init {
listenToBackgroundColor() // Error here
}
private suspend fun listenToBackgroundColor() {
viewModel.colorFlow.collect {
val newColor = Color.parseColor(it)
sceneView.setBackgroundColor(newColor)
}
}
}
My ViewModel:
interface FlutterSceneViewModelType {
var colorFlow: MutableStateFlow<String>
}
class FlutterSceneViewModel(private val database: Database): FlutterSceneViewModelType, ViewModel() {
override var colorFlow = MutableStateFlow<String>("#FFFFFF")
init {
listenToBackgroundColorFlow()
}
private fun listenToBackgroundColorFlow() {
database.backgroundColorFlow.watch {
colorFlow.value = it.hex
}
}
}
the .watch call is a helper I have added so that this can be exposed to iOS using Kotlin multi-platform, it looks as follows but I can use collect instead if necessary:
fun <T> Flow<T>.asCommonFlow(): CommonFlow<T> = CommonFlow(this)
class CommonFlow<T>(private val origin: Flow<T>) : Flow<T> by origin {
fun watch(block: (T) -> Unit): Closeable {
val job = Job()
onEach {
block(it)
}.launchIn(CoroutineScope(Dispatchers.Main + job))
return object : Closeable {
override fun close() {
job.cancel()
}
}
}
}
I resolved this by using viewModel context:
private fun listenToBackgroundColor() {
viewModel.colorFlow.onEach {
val newColor = Color.parseColor(it)
sceneView.setBackgroundColor(newColor)
}.launchIn(viewModel.viewModelScope)
}
I had to import the following into my ViewModel:
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
from:
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0")
I'm playing with coroutine channels and I wanted to implemented a polling test project. The idea is that a viewmodel will listen for data from a repository that polls an endpoint repeatedly.
When I pass a coroutineScope to the repository, the polling works, however when I create a new coroutineSCope in the repository, I see the data being injected into the channel, but it's not received on the viewmodel.
So this works:
class PollingViewModel : ViewModel() {
val counter = MutableLiveData<String>().apply { value = "uninitialized" }
private val repository = Repository()
init {
viewModelScope.launch {
val channel = repository.poll(this /* scope */)
channel.consumeEach {
Log.d("foo", "Viewmodel received [$it]")
counter.postValue(it.toString())
}
}
}
}
class Repository {
private var startValue = 0
suspend fun poll(coroutineScope: CoroutineScope) =
coroutineScope.produce(capacity = Channel.CONFLATED) {
while (true) {
Log.d("foo", "Sending value [$startValue]")
send(startValue++)
delay(POLLING_PERIOD_MILLIS)
}
}
companion object {
private const val POLLING_PERIOD_MILLIS = 1000L
}
}
But this does not (viewmodel does not receive anything):
class PollingViewModel : ViewModel() {
val counter = MutableLiveData<String>().apply { value = "uninitialized" }
private val repository = Repository()
init {
viewModelScope.launch {
repository.poll().consumeEach {
Log.d("foo", "Viewmodel received [$it]")
counter.postValue(it.toString())
}
}
}
}
class Repository {
private var startValue = 0
suspend fun poll() = coroutineScope {
produce(capacity = Channel.CONFLATED) {
while (true) {
Log.d("foo", "Sending value [$startValue]")
send(startValue++)
delay(POLLING_PERIOD_MILLIS)
}
}
}
companion object {
private const val POLLING_PERIOD_MILLIS = 1000L
}
}
What is the issue with creating a coroutineScope at the repository level?
Looks like the solution is to create a new CoroutineContext in the repository:
class Repository {
private var startValue = 0
private val context: CoroutineContext by lazy(LazyThreadSafetyMode.NONE) {
Job() + Dispatchers.IO
}
suspend fun poll(): ReceiveChannel<Int> = coroutineScope {
produce(
context = context,
capacity = Channel.CONFLATED
) {
while (true) {
send(startValue++)
delay(POLLING_PERIOD_MILLIS)
}
}
}
companion object {
private const val POLLING_PERIOD_MILLIS = 1000L
}
}
I am rewriting some Java Vertx asynch code using Kotlin coroutines for learning purposes. However, when I try to test a simple HTTP call, the coroutine based test hangs forever and I really don't understand where is the issue. Here a reproducer:
#RunWith(VertxUnitRunner::class)
class HelloWorldTest {
private val vertx: Vertx = Vertx.vertx()
#Before
fun setUp(context: TestContext) {
// HelloWorldVerticle is a simple http server that replies "Hello, World!" to whatever call
vertx.deployVerticle(HelloWorldVerticle::class.java!!.getName(), context.asyncAssertSuccess())
}
// ORIGINAL ASYNC TEST HERE. IT WORKS AS EXPECTED
#Test
fun testAsync(context: TestContext) {
val atc = context.async()
vertx.createHttpClient().getNow(8080, "localhost", "/") { response ->
response.handler { body ->
context.assertTrue(body.toString().equals("Hello, World!"))
atc.complete()
}
}
}
// First attempt, it hangs forever, the response is never called
#Test
fun testSync1(context: TestContext) = runBlocking<Unit> {
val atc = context.async()
val body = await<HttpClientResponse> {
vertx.createHttpClient().getNow(8080, "localhost", "/", { response -> response.handler {it}} )
}
context.assertTrue(body.toString().equals("Hello, World!"))
atc.complete()
}
// Second attempt, it hangs forever, the response is never called
#Test
fun testSync2(context: TestContext) = runBlocking<Unit> {
val atc = context.async()
val response = await<HttpClientResponse> {
vertx.createHttpClient().getNow(8080, "localhost", "/", it )
}
response.handler { body ->
context.assertTrue(body.toString().equals("Hello, World!"))
atc.complete()
}
}
suspend fun <T> await(callback: (Handler<T>) -> Unit) =
suspendCoroutine<T> { cont ->
callback(Handler { result: T ->
cont.resume(result)
})
}
}
Is everyone able to figure out the issue?
It seems to me that your code have several problems:
you may running the test before the http-server got deployed
I believe that since you execute your code inside runBlocking you are blocking the event loop from completing the request.
Finally, I will advise you to use the HttpClienctResponse::bodyHandler method instead of HttpClientResponse::handler as the handler may receive partial data.
Here is an alternative solution that works fine:
import io.vertx.core.AbstractVerticle
import io.vertx.core.Future
import io.vertx.core.Handler
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
import io.vertx.core.http.HttpClientResponse
import kotlin.coroutines.experimental.Continuation
import kotlin.coroutines.experimental.EmptyCoroutineContext
import kotlin.coroutines.experimental.startCoroutine
import kotlin.coroutines.experimental.suspendCoroutine
inline suspend fun <T> await(crossinline callback: (Handler<T>) -> Unit) =
suspendCoroutine<T> { cont ->
callback(Handler { result: T ->
cont.resume(result)
})
}
fun <T : Any> async(code: suspend () -> T) = Future.future<T>().apply {
code.startCoroutine(object : Continuation<T> {
override val context = EmptyCoroutineContext
override fun resume(value: T) = complete()
override fun resumeWithException(exception: Throwable) = fail(exception)
})
}
fun main(args: Array<String>) {
async {
val vertx: Vertx = Vertx.vertx()
//0. take the current context
val ctx = vertx.getOrCreateContext()
//1. deploy the http server
await<Unit> { cont ->
vertx.deployVerticle(object : AbstractVerticle() {
override fun start() {
vertx.createHttpServer()
.requestHandler { it.response().end("Hello World") }
.listen(7777) { ctx.runOnContext { cont.handle(Unit) } }
//note that it is important tp complete the handler in the correct context
}
})
}
//2. send request
val response: HttpClientResponse = await { vertx.createHttpClient().getNow(7777, "localhost", "/", it) }
//3. await response
val body = await<Buffer> { response.bodyHandler(it) }
println("received $body")
}
}