com.huanshankeji.vertx.core.CombinedVerticle.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-common-vertx Show documentation
Show all versions of kotlin-common-vertx Show documentation
Huanshankeji's common code libraries in Kotlin
package com.huanshankeji.vertx.core
import io.vertx.core.*
interface CombinedVerticleFunctions : Verticle {
val subVerticles: List
fun subVerticlesInit(vertx: Vertx, context: Context) {
for (verticle in subVerticles)
verticle.init(vertx, context)
}
fun subVerticlesStart(startPromise: Promise) =
subVerticles.runAllWithPromise({ start(it) }, startPromise)
fun subVerticlesStop(stopPromise: Promise) =
subVerticles.runAllWithPromise({ stop(it) }, stopPromise)
fun subVerticlesStartUnit(startPromise: Promise) =
@Suppress("UNCHECKED_CAST")
subVerticlesStart(startPromise as Promise)
fun subVerticlesStopUnit(stopPromise: Promise) =
@Suppress("UNCHECKED_CAST")
subVerticlesStop(stopPromise as Promise)
fun List.runAllWithPromise(block: E.(Promise: Promise) -> Unit, promise: Promise) {
CompositeFuture.all(map {
@Suppress("NAME_SHADOWING") val promise = Promise.promise()
vertx.runOnContext { _ -> it.block(promise) }
//it.block(promise)
promise.future()
}).onComplete {
promise.handle(it.map { null })
}
}
}
// This class should preferably be used if multiple `Verticle`s don't have any shared resources. If they do, consider combining them using other ways of abstraction for better efficiency.
open class CombinedVerticle(override val subVerticles: List) : AbstractVerticle(), CombinedVerticleFunctions {
override fun init(vertx: Vertx, context: Context) {
super.init(vertx, context)
subVerticlesInit(vertx, context)
}
override fun start(startPromise: Promise) =
subVerticlesStart(startPromise)
override fun stop(stopPromise: Promise) =
subVerticlesStop(stopPromise)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy