@Around方面和Kotlin暂停功能

时间:2019-01-03 02:57:44

标签: kotlin aop aspectj kotlin-coroutines

我尝试了解如何为Kotlin的暂停功能创建@Around方面(例如,测量在此功能上花费的时间或自定义@Transactional方面):

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

由于此函数具有暂停函数调用,因此@Around方面的procees函数将在delay()调用之前被调用。但是显然,我想衡量该功能所花费的全部时间。

什么是正确的解决方法?也许我可以以某种方式订阅该方法的最后一个续集,还是这样?

2 个答案:

答案 0 :(得分:1)

我认为您可以轻松解决问题,如果要测量功能的执行时间,可以使用以下内置功能来实现:

val time = measureTimeMillis {
    // yourSuperFunc()
}

此外,您可以使用measureNanoTime。有关完整参考,请参见here

答案 1 :(得分:0)

https://github.com/spring-projects/spring-framework/issues/22462对此进行了跟踪,很可能会在5.3中修复

@Transactional也会发生相同的问题。

相关问题