1

我有一个简单的 Kotlin 应用程序,它通过 Ktor 每 10 秒调用一次 Web 服务,并使用协程来执行此操作。在协程中,当我使用 delay() 函数暂停 10 秒时,出现以下错误。当我删除 delay() 函数时,代码工作正常。关于为什么使用 delay() 函数会导致此错误的任何想法?

编码:

@OptIn(ExperimentalTime::class)
class NOAAUpdater(private val timerScope: CoroutineScope) {

    @OptIn(InternalCoroutinesApi::class)
    fun start(): Job {
        return timerScope.launch {
            while (isActive) {
                val client = HttpClient(CIO)
                val response: String = client.get(url)
                client.close()
                logger.debug { response }
                delay(10000)
            }
        }
    }
}
@OptIn(ExperimentalTime::class)
suspend fun main() {

    logger.info { "App Started" }
    val noaaUpdater = NOAAUpdater(CoroutineScope(Dispatchers.Default))
    noaaUpdater.start()

    

}

错误:

Exception in thread "DefaultDispatcher-worker-1" java.lang.NoSuchMethodError: kotlinx.coroutines.DelayKt.delay-p9JZ4hM(JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
    at com.me.starter.NOAAUpdater$start$2.invokeSuspend(NOAAUpdater.kt:41)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:191)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:147)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:15)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:93)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:191)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:147)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:15)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:93)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
4

0 回答 0