简体   繁体   中英

Server uptime in Ktor

How do I calculate server uptime in Ktor. In nodejs, there is process.uptime() which returns server uptime. I don't see an equivalent method in Ktor. Any suggestions?

Import:

import kotlin.time.toDuration
import kotlin.time.DurationUnit
import java.lang.management.ManagementFactory

You can try with ManagementFactory api to get uptime of current running process and convert into duration like below.

routing {
    get("/") {
        val millis = ManagementFactory.getRuntimeMXBean().uptime
        val duration = millis.toDuration(DurationUnit.MILLISECONDS)
        call.respond(duration.toString())
    }
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM