GH-211: Provide the ability to exclude global retryListeners (#392)

* GH-211: Provide the ability to exclude global retryListeners

Fixes
https://github.com/spring-projects/spring-retry/issues/211

* Javadoc polishing.

* Javadoc polishing.

---------

Co-authored-by: akenra <ake.ra@yandex.ru>
Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
akenra
2023-11-21 19:48:11 +03:00
committed by GitHub
parent 7516219c44
commit 8f4c603e5e
3 changed files with 73 additions and 3 deletions

View File

@@ -75,6 +75,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @author Roman Akentev
* @since 1.1
*/
public class AnnotationAwareRetryOperationsInterceptor implements IntroductionInterceptor, BeanFactoryAware {
@@ -323,6 +324,9 @@ public class AnnotationAwareRetryOperationsInterceptor implements IntroductionIn
}
private RetryListener[] getListenersBeans(String[] listenersBeanNames) {
if (listenersBeanNames.length == 1 && "".equals(listenersBeanNames[0].trim())) {
return new RetryListener[0];
}
RetryListener[] listeners = new RetryListener[listenersBeanNames.length];
for (int i = 0; i < listeners.length; i++) {
listeners[i] = this.beanFactory.getBean(listenersBeanNames[i], RetryListener.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
@@ -31,6 +31,7 @@ import org.springframework.core.annotation.AliasFor;
* @author Artem Bilan
* @author Gary Russell
* @author Maksim Kita
* @author Roman Akentev
* @since 1.1
*
*/
@@ -165,7 +166,9 @@ public @interface Retryable {
/**
* Bean names of retry listeners to use instead of default ones defined in Spring
* context
* context. If this attribute is set to an empty string {@code ""}, it will
* effectively exclude all retry listeners, including with the default listener beans,
* from being used.
* @return retry listeners bean names
*/
String[] listeners() default {};

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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.
@@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
* @author Gary Russell
* @author Henning Pöttker
* @author Roman Akentev
*
*/
public class EnableRetryWithListenersTests {
@@ -55,6 +56,16 @@ public class EnableRetryWithListenersTests {
context.close();
}
@Test
public void excludedListeners() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestConfigurationExcludedListeners.class);
ServiceWithExcludedListeners service = context.getBean(ServiceWithExcludedListeners.class);
service.service();
assertThat(context.getBean(TestConfigurationExcludedListeners.class).count).isEqualTo(0);
context.close();
}
@Configuration
@EnableRetry(proxyTargetClass = true)
protected static class TestConfiguration {
@@ -116,6 +127,41 @@ public class EnableRetryWithListenersTests {
}
@Configuration
@EnableRetry(proxyTargetClass = true)
protected static class TestConfigurationExcludedListeners {
private int count = 0;
@Bean
public ServiceWithExcludedListeners service() {
return new ServiceWithExcludedListeners();
}
@Bean
public RetryListener listener1() {
return new RetryListener() {
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
Throwable throwable) {
count++;
}
};
}
@Bean
public RetryListener listener2() {
return new RetryListener() {
@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;
@@ -150,4 +196,21 @@ public class EnableRetryWithListenersTests {
}
protected static class ServiceWithExcludedListeners {
private int count = 0;
@Retryable(backoff = @Backoff(delay = 1000), listeners = "")
public void service() {
if (count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return count;
}
}
}