I'm having a problem using spring boot and spring retry, the @Recover annotation method is not being called when making all the possible attempts.
I'm using spring with kotlin.
My Application servlet container:
class ServletInitializer : SpringBootServletInitializer() {
override fun configure(application: SpringApplicationBuilder) : SpringApplicationBuilder {
return application.sources(SecurityServicesApplication::class.java)
}
}
My Configuration
import org.springframework.context.annotation.Configuration import org.springframework.retry.annotation.EnableRetry
@Configuration
@EnableRetry
class RetryConfig
Updated
My Service
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")
}
}
My Error Log:
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]
Solution [with the help of @Gary Russell]
The return of the @Recover method must be the same as the @Retryable method
fun recover(ex: S@RecoverecurityException, oauth2RestTemplate: OAuth2RestTemplate ) : OAuth2AccessToken {
print("##############################################################################################sssss# SecurityException")
throw br.com.totvs.security.commons.exception.SecurityException("")
}