@Around方面和 Kotlin 暂停功能



我试图了解如何为 Kotlin 的暂停函数创建一个@Around方面(例如,测量在此函数中花费的时间,或用于自定义@Transactional方面):

@Timed("my-timer")
suspend fun test() {
    println("before")
    delay(50) // invokes ProceedingJoinPoint#proceed() before this line
    println("after")
}

由于此函数具有挂起函数调用,因此@Around方面的 continue 函数将在 delay() 调用之前被调用。但显然,我想衡量在该函数中花费的全部时间。

解决它的正确方法是什么?也许我可以以某种方式订阅该方法中的最后一个延续,或者像那样?

我认为您可以轻松解决问题,如果要测量函数的执行时间,可以使用这样的内置功能来做到这一点:

val time = measureTimeMillis {
    // yourSuperFunc()
}

此外,您可以使用 measureNanoTime .有关完整参考,请参阅此处。

这由 https://github.com/spring-projects/spring-framework/issues/22462 跟踪,很可能会在 5.3 中修复

同样的问题发生在@Transactional.

最新更新