Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 使用CoroutineVerticle构建非阻塞VertX服务器_Kotlin_Vert.x_Kotlin Coroutines - Fatal编程技术网

Kotlin 使用CoroutineVerticle构建非阻塞VertX服务器

Kotlin 使用CoroutineVerticle构建非阻塞VertX服务器,kotlin,vert.x,kotlin-coroutines,Kotlin,Vert.x,Kotlin Coroutines,我正在试验VertX+例程,只想检查一下这个设置是否在任何时候阻塞,或者是否有我需要注意的潜在问题 例如,runBlocking在本例中是否正确使用,还是应该执行deployVerticle?然后在requestHandler中,我正在执行GlobalScope.launch,这似乎是不鼓励的,这里使用的正确范围是什么 我已将VertX 4.0.0-milestone5添加到我的Gradle构建脚本中,我没有使用VertX Web: val vertxVersion =

我正在试验VertX+例程,只想检查一下这个设置是否在任何时候阻塞,或者是否有我需要注意的潜在问题

例如,
runBlocking
在本例中是否正确使用,还是应该执行
deployVerticle
?然后在requestHandler中,我正在执行GlobalScope.launch,这似乎是不鼓励的,这里使用的正确范围是什么

我已将VertX 4.0.0-milestone5添加到我的Gradle构建脚本中,我没有使用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")
Routing.kt内部
我有以下设置:

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()
        }
    }

}
然后在
main()中使用此
Routing.setup

对象服务器{
private val log=LoggerFactory.getLogger(this.javaClass)
@JvmStatic
@实验时间
fun main(args:Array)=运行阻塞{
....
//设置路由
路由设置(
端口=如果(ENV.ENV==本地){
5555
}否则{
80
},
) 

Kotlin与Vert.x集成的全部要点是,您不必使用
GlobalScope.launch

以下是如何实现这一目标的一个简单示例:

fun main(){
val vertx=vertx.vertx()
vertx.deployVerticle(“服务器”)
}
类服务器:CoroutineVerticle(){
覆盖暂停乐趣开始(){
createHttpServer().requestHandler{req->
//您已经可以访问所有协同程序生成器
发射{
//在此范围内,可以使用挂起函数
延迟(1000)
req.response().end(“完成!”)
}
}.听(8888)
}
}

Kotlin与Vert.x集成的全部要点是,您不必使用
GlobalScope.launch

以下是如何实现这一目标的一个简单示例:

fun main(){
val vertx=vertx.vertx()
vertx.deployVerticle(“服务器”)
}
类服务器:CoroutineVerticle(){
覆盖暂停乐趣开始(){
createHttpServer().requestHandler{req->
//您已经可以访问所有协同程序生成器
发射{
//在此范围内,可以使用挂起函数
延迟(1000)
req.response().end(“完成!”)
}
}.听(8888)
}
}

我不得不更改一行,即
vertx.deployVerticle(Server())
,然后它就完美地工作了。在我的问题中,launch会触发错误,而在您的示例中,它是有效的,所以我们将进行一些重构,以利用deployVerticle样式。感谢您伟大的回答!我不得不更改一行,即
vertx.deployVerticle(Server())
然后它就完美地工作了。在我的问题中,launch会触发错误,而在您的示例中它是有效的,所以我们将进行一些重构,以利用deployVerticle样式。感谢您给出的好答案!
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
            },
        )