Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot @retryable not retrying

The following code is not retrying. What am I missing?

@EnableRetry
@SpringBootApplication
public class App implements CommandLineRunner
{
    .........
    .........


    @Retryable()
    ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception
    {
        System.out.println("try!");
        throw new Exception();
        //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class);
    }

I have added the following to the pom.xml.

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

I also tried providing different combinations of arguments to @Retryable.

@Retryable(maxAttempts=10,value=Exception.class,backoff=@Backoff(delay = 2000,multiplier=2))

Thanks.

like image 871
engg Avatar asked Jul 05 '16 20:07

engg


People also ask

What is @retryable in spring boot?

Spring Retry provides the ability to automatically re-invoke a failed operation. This is helpful when errors may be transient in nature. For example, a momentary network glitch, network outage, server down, or deadlock. You can configure the. spring-retry.

Does spring retry block thread?

Does the default spring-retry implementation block threads while retrying? The implementation in github indicates that it does.

What is RetryContext?

public interface RetryContext extends org.springframework.core.AttributeAccessor. Low-level access to ongoing retry operation. Normally not needed by clients, but can be used to alter the course of the retry, e.g. force an early termination.

What is backoff in @retryable?

The idea behind using exponential backoff with retry is that instead of retrying after waiting for a fixed amount of time, we increase the waiting time between reties after each retry failure. For example, when the request fails the first time, we retry after one second.


2 Answers

In spring boot 2.0.2 Release, I have observed that the @Retryable is not working if you have retryable and called method in same class. On debugging found that the pointcut is not getting built properly. For now, the workaround for this problem is that we need to write the method in a different class and call it.

Working Example could be found here.

like image 119
nkharche Avatar answered Sep 24 '22 17:09

nkharche


For the @Retryable annotation on the method to be discovered it needs to be called correctly from an initialised context. Is the method invoked from a bean from the spring context or called by other means?

If testing this is your runner using the SpringJunit4ClassRunner?

like image 45
UserF40 Avatar answered Sep 23 '22 17:09

UserF40