Skip to content

Commit

Permalink
Find @retryable on Target Class
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
garyrussell authored and Dave Syer committed Sep 20, 2016
1 parent 660c01a commit ce3621e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,7 @@
*
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @since 1.1
*
*/
Expand Down Expand Up @@ -130,6 +131,9 @@ private MethodInterceptor getDelegate(Object target, Method method) {
if (retryable == null) {
retryable = AnnotationUtils.findAnnotation(method.getDeclaringClass(), Retryable.class);
}
if (retryable == null) {
retryable = findAnnotationOnTarget(target, method);
}
if (retryable == null) {
return this.delegates.put(method, null);
}
Expand All @@ -150,6 +154,16 @@ else if (retryable.stateful()) {
return this.delegates.get(method);
}

private Retryable findAnnotationOnTarget(Object target, Method method) {
try {
Method targetMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
return AnnotationUtils.findAnnotation(targetMethod, Retryable.class);
}
catch (Exception e) {
return null;
}
}

private MethodInterceptor getStatelessInterceptor(Object target, Method method, Retryable retryable) {
return RetryInterceptorBuilder.stateless()
.retryPolicy(getRetryPolicy(retryable))
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@
/**
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @since 1.1
*/
public class EnableRetryTests {
Expand Down Expand Up @@ -145,6 +146,16 @@ public void testExternalInterceptor() {
context.close();
}

@Test
public void testInterface() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TheInterface service = context.getBean(TheInterface.class);
service.service1();
service.service2();
assertEquals(4, service.getCount());
context.close();
}

@Configuration
@EnableRetry(proxyTargetClass = true)
protected static class TestProxyConfiguration {
Expand Down Expand Up @@ -216,6 +227,11 @@ public Foo foo() {
return new Foo();
}

@Bean
public TheInterface anInterface() {
return new TheClass();
}

}

protected static class Service {
Expand Down Expand Up @@ -358,4 +374,41 @@ private static class Foo {

}

public static interface TheInterface {

void service1();

@Retryable
void service2();

int getCount();

}

public static class TheClass implements TheInterface {

private int count = 0;

@Override
@Retryable
public void service1() {
if (count++ < 1) {
throw new RuntimeException("Planned");
}
}

@Override
public void service2() {
if (count++ < 3) {
throw new RuntimeException("Planned");
}
}

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

}

}

0 comments on commit ce3621e

Please sign in to comment.