Does @Retryable work with JDK Dynamic Proxies?

1.5k views Asked by At

I've been experimenting with the spring-retry project. I have successfully used it's @Retryable features but I am unable to get it to work using a JDK Dynamic Proxy.

I'm using a the following code snippet in my tests.

@Configuration
@EnableRetry
public class TestConfig {
    @Bean
    public MethodInterceptor retryInterceptor() {
        return RetryInterceptorBuilder.stateful().maxAttempts(3).build();
    }
}

@Service
public class RetryableServiceImpl implements RetryableService {

    private int count = 0;

    @Retryable(RuntimeException.class)
    @Override
    public void service() {

        if (count++ < 2) {
            throw new RuntimeException("Planned");
        }
    }

    @Override
    public int getCount() {
        return count;
    }

}

@ContextConfiguration(...)
public class RetryableServiceImplTest ... {

    @Autowired
    private RetryableService retryableService;

    @Test
    public void test() {
        assertTrue(AopUtils.isAopProxy(retryableService));
        assertTrue(AopUtils.isJdkDynamicProxy(retryableService));
        assertFalse(AopUtils.isCglibProxy(retryableService));
        retryableService.service();
        assertEquals(3, retryableService.getCount());
    }

}

Sample project available here: https://github.com/maddenj-ie/retry.git

So, my questions are

  1. Should this work using either cglib or JDK dynamic proxies?

  2. If so, what is wrong with my setup?

Thanks for the help.

Regards, Joe

1

There are 1 answers

0
Joseph Madden On BEST ANSWER

After some further investigation, to answer my own questions:

  1. It does work with both proxy mechanisms.

  2. The @Retryable annotation must be applied to the interface instead of the class in order for it to be applied correctly.

Debugging the AnnotationAwareRetryOperationsInterceptor help me understand this.