Ensure label is used in StatefulRetryOperationsInterceptor
This commit is contained in:
@@ -326,6 +326,8 @@ public abstract class RetryInterceptorBuilder<T extends MethodInterceptor> {
|
||||
|
||||
private MethodArgumentsKeyGenerator keyGenerator;
|
||||
|
||||
private String label;
|
||||
|
||||
@Override
|
||||
public CircuitBreakerInterceptorBuilder retryOperations(
|
||||
RetryOperations retryOperations) {
|
||||
@@ -353,7 +355,7 @@ public abstract class RetryInterceptorBuilder<T extends MethodInterceptor> {
|
||||
}
|
||||
|
||||
public CircuitBreakerInterceptorBuilder label(String label) {
|
||||
super.recoverer(this.recoverer);
|
||||
this.label = label;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -371,6 +373,9 @@ public abstract class RetryInterceptorBuilder<T extends MethodInterceptor> {
|
||||
if (this.keyGenerator != null) {
|
||||
this.interceptor.setKeyGenerator(this.keyGenerator);
|
||||
}
|
||||
if (this.label != null) {
|
||||
this.interceptor.setLabel(this.label);
|
||||
}
|
||||
this.interceptor.setRollbackClassifier(new BinaryExceptionClassifier(false));
|
||||
return this.interceptor;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.retry.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.retry.RetryCallback;
|
||||
import org.springframework.retry.RetryContext;
|
||||
import org.springframework.retry.RetryListener;
|
||||
import org.springframework.retry.listener.RetryListenerSupport;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class EnableRetryWithListenersTests {
|
||||
|
||||
@Test
|
||||
public void vanilla() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
TestConfiguration.class);
|
||||
Service service = context.getBean(Service.class);
|
||||
service.service();
|
||||
assertEquals(1, context.getBean(TestConfiguration.class).count);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableRetry(proxyTargetClass = true)
|
||||
protected static class TestConfiguration {
|
||||
|
||||
private int count = 0;
|
||||
|
||||
@Bean
|
||||
public Service service() {
|
||||
return new Service();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RetryListener listener() {
|
||||
return new RetryListenerSupport() {
|
||||
@Override
|
||||
public <T, E extends Throwable> void close(RetryContext context,
|
||||
RetryCallback<T, E> callback, Throwable throwable) {
|
||||
count++;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static class Service {
|
||||
|
||||
private int count = 0;
|
||||
|
||||
@Retryable(backoff = @Backoff(delay = 1000))
|
||||
public void service() {
|
||||
if (count++ < 2) {
|
||||
throw new RuntimeException("Planned");
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,6 +32,9 @@ import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.target.SingletonTargetSource;
|
||||
import org.springframework.retry.ExhaustedRetryException;
|
||||
import org.springframework.retry.RetryCallback;
|
||||
import org.springframework.retry.RetryContext;
|
||||
import org.springframework.retry.listener.RetryListenerSupport;
|
||||
import org.springframework.retry.policy.AlwaysRetryPolicy;
|
||||
import org.springframework.retry.policy.NeverRetryPolicy;
|
||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||
@@ -51,14 +54,25 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
|
||||
private Transformer transformer;
|
||||
|
||||
private RetryContext context;
|
||||
|
||||
private static int count;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
interceptor = new StatefulRetryOperationsInterceptor();
|
||||
service = (Service) ProxyFactory.getProxy(Service.class, new SingletonTargetSource(new ServiceImpl()));
|
||||
transformer = (Transformer) ProxyFactory.getProxy(Transformer.class, new SingletonTargetSource(
|
||||
new TransformerImpl()));
|
||||
retryTemplate.registerListener(new RetryListenerSupport() {
|
||||
@Override
|
||||
public <T, E extends Throwable> void close(RetryContext context,
|
||||
RetryCallback<T, E> callback, Throwable throwable) {
|
||||
StatefulRetryOperationsInterceptorTests.this.context = context;
|
||||
}
|
||||
});
|
||||
interceptor.setRetryOperations(retryTemplate);
|
||||
service = (Service) ProxyFactory.getProxy(Service.class,
|
||||
new SingletonTargetSource(new ServiceImpl()));
|
||||
transformer = (Transformer) ProxyFactory.getProxy(Transformer.class,
|
||||
new SingletonTargetSource(new TransformerImpl()));
|
||||
count = 0;
|
||||
}
|
||||
|
||||
@@ -71,11 +85,29 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultInterceptorWithLabel() throws Exception {
|
||||
interceptor.setLabel("FOO");
|
||||
((Advised) service).addAdvice(interceptor);
|
||||
try {
|
||||
service.service("foo");
|
||||
fail("Expected Exception.");
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
assertEquals("FOO", context.getAttribute(RetryContext.NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultTransformerInterceptorSunnyDay() throws Exception {
|
||||
((Advised) transformer).addAdvice(interceptor);
|
||||
@@ -85,7 +117,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
}
|
||||
@@ -101,7 +134,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
}
|
||||
@@ -117,15 +151,17 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
});
|
||||
interceptor.setRetryOperations(retryTemplate);
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, Collections
|
||||
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2,
|
||||
Collections.<Class<? extends Throwable>, Boolean>singletonMap(
|
||||
Exception.class, true)));
|
||||
try {
|
||||
service.service("foo");
|
||||
fail("Expected Exception.");
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
service.service("foo");
|
||||
@@ -137,15 +173,17 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
public void testTransformerWithSuccessfulRetry() throws Exception {
|
||||
((Advised) transformer).addAdvice(interceptor);
|
||||
interceptor.setRetryOperations(retryTemplate);
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, Collections
|
||||
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
|
||||
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2,
|
||||
Collections.<Class<? extends Throwable>, Boolean>singletonMap(
|
||||
Exception.class, true)));
|
||||
try {
|
||||
transformer.transform("foo");
|
||||
fail("Expected Exception.");
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
Collection<String> result = transformer.transform("foo");
|
||||
@@ -164,7 +202,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
try {
|
||||
@@ -174,7 +213,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
catch (ExhaustedRetryException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Retry exhausted"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Retry exhausted"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
}
|
||||
@@ -190,7 +230,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
interceptor.setRecoverer(new MethodInvocationRecoverer<Object>() {
|
||||
@@ -214,7 +255,8 @@ public class StatefulRetryOperationsInterceptorTests {
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
|
||||
assertTrue("Wrong message: " + message,
|
||||
message.startsWith("Not enough calls"));
|
||||
}
|
||||
assertEquals(1, count);
|
||||
interceptor.setRecoverer(new MethodInvocationRecoverer<Collection<String>>() {
|
||||
|
||||
Reference in New Issue
Block a user