我想运行一个定期任务。在Spring MVC中,它可以完美地工作。现在我想集成Spring Webflux + Kotlin协程。如何在@Scheduled方法中调用挂起的函数?我想让它等到挂起的函数完成。
/// This function starts every 00:10 UTC
@Scheduled(cron = "0 10 0 * * *", zone = "UTC")
fun myScheduler() {
// ???
}
suspend fun mySuspendedFunction() {
// business logic
}发布于 2021-06-07 07:45:16
fun myScheduler() {
runBlocking {
mySuspendedFunction()
}
}这样,协程将在被阻塞的线程中运行。如果你需要在不同的线程中运行代码或者并行执行几个协程,你可以将一个dispatcher (例如Dispatchers.Default,Dispatchers.IO)传递给runBlocking()或者使用withContenxt()。
https://stackoverflow.com/questions/67864495
复制相似问题