5

I have a simple method in a Service in a SpringBoot Application. I have the retry mechanism setup for that method using @Retryable.
I am trying integration tests for the method in the service and the retries are not happening when the method throws an exception. The method gets executed only once.

public interface ActionService { 

@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 2000))
public void perform() throws Exception;

}



@Service
public class ActionServiceImpl implements ActionService {

@Override   
public void perform() throws Exception() {

   throw new Exception();
  } 
}



@SpringBootApplication
@Import(RetryConfig.class)
public class MyApp {

public static void main(String[] args) throws Exception {
    SpringApplication.run(MyApp.class, args);
  }
}



@Configuration
@EnableRetry
public class RetryConfig {

@Bean
public ActionService actionService() { return new ActionServiceImpl(); }

}



@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration( classes= {MyApp.class}) 
@IntegrationTest({"server.port:0", "management.port:0"})
public class MyAppIntegrationTest {

@Autowired
private ActionService actionService;

public void testAction() {

  actionService.perform();

}
user3451476
  • 297
  • 1
  • 4
  • 17
  • I don't think `@Retryable` is inherited. Try moving it to the bean service method instead of the interface method – Ori Dar Jul 22 '15 at 05:30

2 Answers2

5

Your annotation @EnableRetry is at the wrong place, instead of putting it on the ActionService interface you should place it with a Spring Java based @Configuration class, in this instance with the MyApp class. With this change the retry logic should work as expected. Here is a blog post that I had written on if you are interested in more details - http://biju-allandsundry.blogspot.com/2014/12/spring-retry-ways-to-integrate-with.html

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • This being a SpringBoot Application, should I have the Service as a bean in the Configuration, apart from the interface itself. I have updated the post with my changes. And it still does not work. – user3451476 Jul 22 '15 at 07:45
  • I tested your classes and it worked for me well, the `actionService` was called three times and then returned with an exception, try printing something in your perform method to see it get called multiple times. – Biju Kunjummen Jul 22 '15 at 15:20
0

Biju Thanks for putting up a link for this, it helped me to resolve a lot of questions. The only thing that I had to seperately was I still had to add 'retryAdvice' as a bean in the spring xml based approach, Also had to make sure I had context annotation enabled and aspectj is available at classpath. After I added these below I could get it working.

    <bean id="retryAdvice"
    class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
</bean>

<context:annotation-config />
<aop:aspectj-autoproxy />
Sabarish
  • 862
  • 1
  • 14
  • 23
  • 2
    `@Configuration` is the same as ``, `@EnableAspectJAutoProxy` is the same as `` and `@EnableRetry` adds `@EnableAspectJAutoProxy`. Adding `spring-boot-starter-aop` as a dependency provides `aspectjweaver`. – WhiteKnight Nov 11 '16 at 10:14