You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring @Retryable注解在Kotlin挂起函数中无法生效的问题咨询

Spring @Retryable注解在Kotlin挂起函数中无法生效的问题咨询

我最近在Kotlin+Gradle+Spring的项目里,尝试给@Service方法添加重试逻辑,发现如果方法是suspend挂起函数的话,@Retryable注解就不生效了。想问问这是正常情况还是我哪里配置漏了?

先给大家看一下能正常工作的测试方法:

@Retryable(maxAttempts = 3, backoff =  Backoff(delay = 1000) )
fun getPerson(): Person {
    val random = (1..2).random()
    return runBlocking {

        println("random=$random,  ${if (random == 1) "exception" else "success"} ")

        if (random == 1) throw IllegalStateException("Exception occurred")

        Person("Name")
    }
}

再看看不生效的挂起方法:

@Retryable(maxAttempts = 3,backoff =  Backoff(delay = 1000))
suspend fun getPersonSuspend(): Person {
    val random = (1..2).random()
    println("random=$random,  ${if (random == 1) "exception" else "success"} ")
    if (random == 1) throw IllegalStateException("Exception occurred")

    return Person("Name")
}

目前的情况是,非挂起的getPerson方法在抛出异常时会按预期触发3次重试,但suspend修饰的getPersonSuspend方法只会执行一次就直接抛出异常,完全没走重试逻辑。

备注:内容来源于stack exchange,提问作者Ekaterina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 08:38:07