From 6b2b20a8147259dc14a24b6e712248c172b6f373 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 21 Jun 2016 12:33:52 +0100 Subject: [PATCH 1/4] Update for 1.1.3 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a224523..077f266 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.retry spring-retry - 1.1.3.BUILD-SNAPSHOT + 1.1.3.RELEASE Spring Retry From b5340eb4503bca067015e04844d9c1d54acd87e5 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 21 Jun 2016 12:34:23 +0100 Subject: [PATCH 2/4] Revert to snapshots --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 077f266..4898954 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.retry spring-retry - 1.1.3.RELEASE + 1.1.4.BUILD-SNAPSHOT Spring Retry From 660c01adc979f6269ed83fe96ca168296b1b1787 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Fri, 26 Dec 2014 11:41:30 -0500 Subject: [PATCH 3/4] GH-19 SimpleRetryPolicy MaxAttempts JavaDocs See GH-19 --- README.md | 2 +- .../interceptor/RetryInterceptorBuilder.java | 2 +- .../retry/policy/SimpleRetryPolicy.java | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 27191bf..5235f34 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ The `SimpleRetryPolicy` just allows a retry on any of a named list of exception ```java SimpleRetryPolicy policy = new SimpleRetryPolicy(); -// Set the max retry attempts +// Set the max attempts including the initial attempt before retrying policy.setMaxAttempts(5); // Retry on all exceptions (this is the default) policy.setRetryableExceptions(new Class[] {Exception.class}); diff --git a/src/main/java/org/springframework/retry/interceptor/RetryInterceptorBuilder.java b/src/main/java/org/springframework/retry/interceptor/RetryInterceptorBuilder.java index f354761..bc2eb32 100644 --- a/src/main/java/org/springframework/retry/interceptor/RetryInterceptorBuilder.java +++ b/src/main/java/org/springframework/retry/interceptor/RetryInterceptorBuilder.java @@ -99,7 +99,7 @@ public abstract class RetryInterceptorBuilder { /** * Apply the max attempts - a SimpleRetryPolicy will be used. Cannot be used if a custom retry operations * or retry policy has been set. - * @param maxAttempts the max attempts. + * @param maxAttempts the max attempts (including the initial attempt). * @return this. */ public RetryInterceptorBuilder maxAttempts(int maxAttempts) { diff --git a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java index f0ca03d..f0421c1 100644 --- a/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java +++ b/src/main/java/org/springframework/retry/policy/SimpleRetryPolicy.java @@ -92,22 +92,23 @@ public class SimpleRetryPolicy implements RetryPolicy { } /** - * Setter for retry attempts. + * Set the number of attempts before retries are exhausted. Includes the initial + * attempt before the retries begin so, generally, will be {@code >= 1}. For example + * setting this property to 3 means 3 attempts total (initial + 2 retries). * - * @param retryAttempts the number of attempts before a retry becomes - * impossible. + * @param maxAttempts the maximum number of attempts including the initial attempt. */ - public void setMaxAttempts(int retryAttempts) { - this.maxAttempts = retryAttempts; + public void setMaxAttempts(int maxAttempts) { + this.maxAttempts = maxAttempts; } /** - * The maximum number of retry attempts before failure. + * The maximum number of attempts before failure. * * @return the maximum number of attempts */ public int getMaxAttempts() { - return maxAttempts; + return this.maxAttempts; } /** @@ -118,6 +119,7 @@ public class SimpleRetryPolicy implements RetryPolicy { * @return true if the last exception was retryable and the number of * attempts so far is less than the limit. */ + @Override public boolean canRetry(RetryContext context) { Throwable t = context.getLastThrowable(); return (t == null || retryForException(t)) && context.getRetryCount() < maxAttempts; @@ -126,6 +128,7 @@ public class SimpleRetryPolicy implements RetryPolicy { /** * @see org.springframework.retry.RetryPolicy#close(RetryContext) */ + @Override public void close(RetryContext status) { } @@ -134,6 +137,7 @@ public class SimpleRetryPolicy implements RetryPolicy { * * @see RetryPolicy#registerThrowable(RetryContext, Throwable) */ + @Override public void registerThrowable(RetryContext context, Throwable throwable) { SimpleRetryContext simpleContext = ((SimpleRetryContext) context); simpleContext.registerThrowable(throwable); @@ -146,6 +150,7 @@ public class SimpleRetryPolicy implements RetryPolicy { * * @see org.springframework.retry.RetryPolicy#open(RetryContext) */ + @Override public RetryContext open(RetryContext parent) { return new SimpleRetryContext(parent); } From ce3621ea1d91580431b58759ec027c55cbcff24c Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 10 Feb 2016 18:28:35 -0500 Subject: [PATCH 4/4] Find @Retryable on Target Class fixes #32 --- ...tationAwareRetryOperationsInterceptor.java | 16 +++++- .../retry/annotation/EnableRetryTests.java | 55 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java b/src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java index 6c46121..e0780e8 100644 --- a/src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java +++ b/src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java @@ -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. @@ -54,6 +54,7 @@ import org.springframework.util.StringUtils; * * @author Dave Syer * @author Artem Bilan + * @author Gary Russell * @since 1.1 * */ @@ -130,6 +131,9 @@ public class AnnotationAwareRetryOperationsInterceptor implements IntroductionIn 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); } @@ -150,6 +154,16 @@ public class AnnotationAwareRetryOperationsInterceptor implements IntroductionIn 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)) diff --git a/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java b/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java index 72d6235..5914447 100644 --- a/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java +++ b/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java @@ -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. @@ -35,6 +35,7 @@ import org.springframework.retry.interceptor.RetryInterceptorBuilder; /** * @author Dave Syer * @author Artem Bilan + * @author Gary Russell * @since 1.1 */ public class EnableRetryTests { @@ -145,6 +146,16 @@ public class EnableRetryTests { 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 { @@ -216,6 +227,11 @@ public class EnableRetryTests { return new Foo(); } + @Bean + public TheInterface anInterface() { + return new TheClass(); + } + } protected static class Service { @@ -358,4 +374,41 @@ public class EnableRetryTests { } + 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; + } + + } + }