【发布时间】:2019-01-18 03:42:38
【问题描述】:
我在使用 spring boot 和 spring retry 时遇到问题,在进行所有可能的尝试时没有调用 @Recover 注释方法。
我在 kotlin 中使用 spring。
我的应用程序 servlet 容器:
class ServletInitializer : SpringBootServletInitializer() {
override fun configure(application: SpringApplicationBuilder) : SpringApplicationBuilder {
return application.sources(SecurityServicesApplication::class.java)
}
}
我的配置
导入 org.springframework.context.annotation.Configuration 导入 org.springframework.retry.annotation.EnableRetry
@Configuration
@EnableRetry
class RetryConfig
更新
我的服务
import security.commons.exception.SecurityException
import org.apache.commons.lang3.exception.ExceptionUtils
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.retry.annotation.Backoff
import org.springframework.retry.annotation.Recover
import org.springframework.retry.annotation.Retryable
import org.springframework.security.oauth2.client.OAuth2RestTemplate
import org.springframework.security.oauth2.common.OAuth2AccessToken
import org.springframework.stereotype.Service
import java.net.ConnectException
@Service
class AuthorizationServerTokenRequester {
val log = LoggerFactory.getLogger(Oauth2Service::class.java)!!
@Value("\${accessTokenUri}")
private val accessTokenUri: String? = null
@Retryable(
value = [SecurityException::class],
maxAttemptsExpression = "\${server.oauthclient.retry.maxAttempts}",
backoff = Backoff(delayExpression = "\${server.oauthclient.retry.delay}"))
fun token(oauth2RestTemplate: OAuth2RestTemplate): OAuth2AccessToken? {
try {
return oauth2RestTemplate.accessToken
} catch (ex: Exception) {
if (ExceptionUtils.getRootCause(ex) is ConnectException) {
log.error("trying again....")
}
throw com.security.commons.exception.SecurityException("")
}
}
@Recover
fun recover(ex: SecurityException) {
print("##############################################################################################sssss# SecurityException")
}
}
我的错误日志:
2018-08-10 11:28:41.802 DEBUG 40168 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor :
Written [{timestamp=Fri Aug 10 11:28:41 BRT 2018, status=500, error=Internal Server Error, exception=org.springframework.retry.ExhaustedRetryException,
message=Cannot locate recovery method; nested exception is security.commons.exception.SecurityException: ,
path=/security/api/v1/oauth2/token}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@21e2d8f6]
解决方案 [在 @Gary Russell 的帮助下]
@Recover 方法的返回必须与@Retryable 方法相同
fun recover(ex: S@RecoverecurityException, oauth2RestTemplate: OAuth2RestTemplate ) : OAuth2AccessToken {
print("##############################################################################################sssss# SecurityException")
throw br.com.totvs.security.commons.exception.SecurityException("")
}
【问题讨论】:
-
我认为
@Retryable应该用在接口而不是类中 -
@lukaszrys 从文档中的github.com/spring-projects/spring-retry 类中,可重试工作正常,恢复未被调用
标签: java rest spring-boot kotlin spring-retry