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
相关产品推荐
相关产品推荐

