Skip to content

Commit

Permalink
spring-projectsGH-211: Provide the ability to exclude global retryLis…
Browse files Browse the repository at this point in the history
…teners

Fixes
spring-projects#211
  • Loading branch information
akenra committed Oct 21, 2023
1 parent c89b951 commit f5ed79b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,21 @@ else if (this.globalListeners != null) {
}

private RetryListener[] getListenersBeans(String[] listenersBeanNames) {
if (listenersBeanNames.length == 1 && "".equals(listenersBeanNames[0].trim())) {
return new RetryListener[] { createNoOpRetryListener() };
}
RetryListener[] listeners = new RetryListener[listenersBeanNames.length];
for (int i = 0; i < listeners.length; i++) {
listeners[i] = this.beanFactory.getBean(listenersBeanNames[i], RetryListener.class);
}
return listeners;
}

private static RetryListener createNoOpRetryListener() {
return new RetryListener() {
};
}

private MethodInvocationRecoverer<?> getRecoverer(Object target, Method method) {
if (target instanceof MethodInvocationRecoverer) {
return (MethodInvocationRecoverer<?>) target;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* @author Artem Bilan
* @author Gary Russell
* @author Maksim Kita
* @author Roman Akentev
* @since 1.1
*
*/
Expand Down Expand Up @@ -165,7 +166,10 @@

/**
* 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 "", a special NoOp retry
* listener will be registered for the annotated method invocation, which will
* effectively exclude all retry listeners, including the default ones, from being
* used.
* @return retry listeners bean names
*/
String[] listeners() default {};
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* @author Dave Syer
* @author Gary Russell
* @author Henning Pöttker
* @author Roman Akentev
*
*/
public class EnableRetryWithListenersTests {
Expand All @@ -55,6 +56,16 @@ public void overrideListener() {
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 {
Expand Down Expand Up @@ -116,6 +127,41 @@ public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T

}

@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;
Expand Down Expand Up @@ -150,4 +196,21 @@ public int getCount() {

}

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;
}

}

}

0 comments on commit f5ed79b

Please sign in to comment.