Skip to content

Commit 8730729

Browse files
authored
Deprecate RetryListenSupport for default methods
Deprecate a `RetryListenerSupport` in favor of `default` methods in `RetryListener` interface
1 parent 61960f3 commit 8730729

File tree

12 files changed

+97
-38
lines changed

12 files changed

+97
-38
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ If there has been an error, it is the last one thrown by the `RetryCallback`.
364364
Note that when there is more than one listener, they are in a list, so there is an order.
365365
In this case, `open` is called in the same order, while `onSuccess`, `onError`, and `close` are called in reverse order.
366366

367-
`RetryListenerSupport` is provided, with no-op implementations; you can extend this class if you don't need to implement all of the `RetryListener` methods.
368-
369367
### Listeners for Reflective Method Invocations
370368

371369
When dealing with methods that are annotated with `@Retryable` or with Spring AOP intercepted methods, Spring Retry allows a detailed inspection of the method invocation within the `RetryListener` implementation.

src/main/java/org/springframework/retry/RetryListener.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
* @author Dave Syer
2525
* @author Gary Russell
26+
* @author Henning Pöttker
2627
*
2728
*/
2829
public interface RetryListener {
@@ -38,7 +39,9 @@ public interface RetryListener {
3839
* @param callback the current {@link RetryCallback}.
3940
* @return true if the retry should proceed.
4041
*/
41-
<T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback);
42+
default <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
43+
return true;
44+
}
4245

4346
/**
4447
* Called after the final attempt (successful or not). Allow the listener to clean up
@@ -49,7 +52,9 @@ public interface RetryListener {
4952
* @param <E> the exception type
5053
* @param <T> the return value
5154
*/
52-
<T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
55+
default <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
56+
Throwable throwable) {
57+
}
5358

5459
/**
5560
* Called after a successful attempt; allow the listener to throw a new exception to
@@ -72,6 +77,8 @@ default <T, E extends Throwable> void onSuccess(RetryContext context, RetryCallb
7277
* @param <T> the return value
7378
* @param <E> the exception to throw
7479
*/
75-
<T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
80+
default <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
81+
Throwable throwable) {
82+
}
7683

7784
}

src/main/java/org/springframework/retry/listener/RetryListenerSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,10 @@
2424
* Empty method implementation of {@link RetryListener}.
2525
*
2626
* @author Dave Syer
27-
*
27+
* @author Henning Pöttker
28+
* @deprecated in favor of the default implementations in {@link RetryListener}
2829
*/
30+
@Deprecated(since = "2.0.1", forRemoval = true)
2931
public class RetryListenerSupport implements RetryListener {
3032

3133
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,

src/main/java/org/springframework/retry/stats/StatisticsListener.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,16 @@
1919
import org.springframework.core.AttributeAccessor;
2020
import org.springframework.retry.RetryCallback;
2121
import org.springframework.retry.RetryContext;
22+
import org.springframework.retry.RetryListener;
2223
import org.springframework.retry.RetryStatistics;
23-
import org.springframework.retry.listener.RetryListenerSupport;
2424
import org.springframework.retry.policy.CircuitBreakerRetryPolicy;
2525

2626
/**
2727
* @author Dave Syer
28+
* @author Henning Pöttker
2829
*
2930
*/
30-
public class StatisticsListener extends RetryListenerSupport {
31+
public class StatisticsListener implements RetryListener {
3132

3233
private final StatisticsRepository repository;
3334

src/test/java/org/springframework/retry/annotation/EnableRetryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
4040
import org.springframework.retry.backoff.Sleeper;
4141
import org.springframework.retry.interceptor.RetryInterceptorBuilder;
42-
import org.springframework.retry.listener.RetryListenerSupport;
4342
import org.springframework.retry.policy.SimpleRetryPolicy;
4443
import org.springframework.retry.support.RetryTemplate;
4544

@@ -57,6 +56,7 @@
5756
* @author Artem Bilan
5857
* @author Gary Russell
5958
* @author Aldo Sinanaj
59+
* @author Henning Pöttker
6060
* @since 1.1
6161
*/
6262
public class EnableRetryTests {
@@ -910,7 +910,7 @@ public int getCount() {
910910

911911
}
912912

913-
public abstract static class OrderedListener extends RetryListenerSupport implements Ordered {
913+
public abstract static class OrderedListener implements RetryListener, Ordered {
914914

915915
}
916916

src/test/java/org/springframework/retry/annotation/EnableRetryWithListenersTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import org.springframework.retry.RetryCallback;
2525
import org.springframework.retry.RetryContext;
2626
import org.springframework.retry.RetryListener;
27-
import org.springframework.retry.listener.RetryListenerSupport;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
3029

3130
/**
3231
* @author Dave Syer
3332
* @author Gary Russell
33+
* @author Henning Pöttker
3434
*
3535
*/
3636
public class EnableRetryWithListenersTests {
@@ -68,7 +68,7 @@ public Service service() {
6868

6969
@Bean
7070
public RetryListener listener() {
71-
return new RetryListenerSupport() {
71+
return new RetryListener() {
7272
@Override
7373
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
7474
Throwable throwable) {
@@ -94,7 +94,7 @@ public ServiceWithOverriddenListener service() {
9494

9595
@Bean
9696
public RetryListener listener1() {
97-
return new RetryListenerSupport() {
97+
return new RetryListener() {
9898
@Override
9999
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
100100
Throwable throwable) {
@@ -105,7 +105,7 @@ public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T
105105

106106
@Bean
107107
public RetryListener listener2() {
108-
return new RetryListenerSupport() {
108+
return new RetryListener() {
109109
@Override
110110
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
111111
Throwable throwable) {

src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import org.springframework.context.support.ClassPathXmlApplicationContext;
3636
import org.springframework.retry.RetryCallback;
3737
import org.springframework.retry.RetryContext;
38+
import org.springframework.retry.RetryListener;
3839
import org.springframework.retry.listener.MethodInvocationRetryListenerSupport;
39-
import org.springframework.retry.listener.RetryListenerSupport;
4040
import org.springframework.retry.policy.NeverRetryPolicy;
4141
import org.springframework.retry.policy.SimpleRetryPolicy;
4242
import org.springframework.retry.support.RetryTemplate;
@@ -47,6 +47,13 @@
4747
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4848
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4949

50+
/**
51+
* @author Dave Syer
52+
* @author Marius Grama
53+
* @author Gary Russell
54+
* @author Stéphane Nicoll
55+
* @author Henning Pöttker
56+
*/
5057
public class RetryOperationsInterceptorTests {
5158

5259
private static int count;
@@ -66,7 +73,7 @@ public void setUp() {
6673
this.interceptor = new RetryOperationsInterceptor();
6774
RetryTemplate retryTemplate = new RetryTemplate();
6875
final AtomicBoolean calledFirst = new AtomicBoolean();
69-
retryTemplate.registerListener(new RetryListenerSupport() {
76+
retryTemplate.registerListener(new RetryListener() {
7077

7178
@Override
7279
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
@@ -83,7 +90,7 @@ public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T
8390
}
8491

8592
});
86-
retryTemplate.registerListener(new RetryListenerSupport() {
93+
retryTemplate.registerListener(new RetryListener() {
8794

8895
@Override
8996
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {

src/test/java/org/springframework/retry/interceptor/StatefulRetryOperationsInterceptorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import org.springframework.retry.ExhaustedRetryException;
3333
import org.springframework.retry.RetryCallback;
3434
import org.springframework.retry.RetryContext;
35+
import org.springframework.retry.RetryListener;
3536
import org.springframework.retry.RetryOperations;
36-
import org.springframework.retry.listener.RetryListenerSupport;
3737
import org.springframework.retry.policy.AlwaysRetryPolicy;
3838
import org.springframework.retry.policy.NeverRetryPolicy;
3939
import org.springframework.retry.policy.SimpleRetryPolicy;
@@ -51,6 +51,7 @@
5151
/**
5252
* @author Dave Syer
5353
* @author Gary Russell
54+
* @author Henning Pöttker
5455
*
5556
*/
5657
public class StatefulRetryOperationsInterceptorTests {
@@ -70,7 +71,7 @@ public class StatefulRetryOperationsInterceptorTests {
7071
@BeforeEach
7172
public void setUp() {
7273
interceptor = new StatefulRetryOperationsInterceptor();
73-
retryTemplate.registerListener(new RetryListenerSupport() {
74+
retryTemplate.registerListener(new RetryListener() {
7475
@Override
7576
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
7677
Throwable throwable) {

src/test/java/org/springframework/retry/listener/RetryListenerSupportTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
import static org.assertj.core.api.Assertions.assertThat;
2222
import static org.assertj.core.api.Assertions.assertThatNoException;
2323

24+
/**
25+
* @author Dave Syer
26+
* @author Gary Russell
27+
* @author Henning Pöttker
28+
*/
29+
@SuppressWarnings("deprecation")
2430
public class RetryListenerSupportTests {
2531

2632
@Test

src/test/java/org/springframework/retry/listener/RetryListenerTests.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
import static org.assertj.core.api.Assertions.assertThat;
3232
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3333
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
34+
import static org.assertj.core.api.Assertions.assertThatNoException;
3435

36+
/**
37+
* @author Dave Syer
38+
* @author Stéphane Nicoll
39+
* @author Gary Russell
40+
* @author Henning Pöttker
41+
*/
3542
public class RetryListenerTests {
3643

3744
RetryTemplate template = new RetryTemplate();
@@ -40,15 +47,43 @@ public class RetryListenerTests {
4047

4148
List<String> list = new ArrayList<>();
4249

50+
@Test
51+
public void testOpenDefaultImplementation() {
52+
var retryListener = new RetryListener() {
53+
};
54+
assertThat(retryListener.open(null, null)).isTrue();
55+
}
56+
57+
@Test
58+
public void testCloseDefaultImplementation() {
59+
var retryListener = new RetryListener() {
60+
};
61+
assertThatNoException().isThrownBy(() -> retryListener.close(null, null, null));
62+
}
63+
64+
@Test
65+
public void testOnSuccessDefaultImplementation() {
66+
var retryListener = new RetryListener() {
67+
};
68+
assertThatNoException().isThrownBy(() -> retryListener.onError(null, null, null));
69+
}
70+
71+
@Test
72+
public void testOnErrorDefaultImplementation() {
73+
var retryListener = new RetryListener() {
74+
};
75+
assertThatNoException().isThrownBy(() -> retryListener.onError(null, null, null));
76+
}
77+
4378
@Test
4479
public void testOpenInterceptors() {
45-
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
80+
template.setListeners(new RetryListener[] { new RetryListener() {
4681
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
4782
count++;
4883
list.add("1:" + count);
4984
return true;
5085
}
51-
}, new RetryListenerSupport() {
86+
}, new RetryListener() {
5287
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
5388
count++;
5489
list.add("2:" + count);
@@ -63,7 +98,7 @@ public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback
6398

6499
@Test
65100
public void testOpenCanVetoRetry() {
66-
template.registerListener(new RetryListenerSupport() {
101+
template.registerListener(new RetryListener() {
67102
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
68103
list.add("1");
69104
return false;
@@ -80,13 +115,13 @@ public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback
80115

81116
@Test
82117
public void testCloseInterceptors() {
83-
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
118+
template.setListeners(new RetryListener[] { new RetryListener() {
84119
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
85120
Throwable t) {
86121
count++;
87122
list.add("1:" + count);
88123
}
89-
}, new RetryListenerSupport() {
124+
}, new RetryListener() {
90125
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
91126
Throwable t) {
92127
count++;
@@ -103,12 +138,12 @@ public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T
103138
@Test
104139
public void testOnError() {
105140
template.setRetryPolicy(new NeverRetryPolicy());
106-
template.setListeners(new RetryListener[] { new RetryListenerSupport() {
141+
template.setListeners(new RetryListener[] { new RetryListener() {
107142
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
108143
Throwable throwable) {
109144
list.add("1");
110145
}
111-
}, new RetryListenerSupport() {
146+
}, new RetryListener() {
112147
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
113148
Throwable throwable) {
114149
list.add("2");
@@ -128,7 +163,7 @@ public <T, E extends Throwable> void onError(RetryContext context, RetryCallback
128163

129164
@Test
130165
public void testCloseInterceptorsAfterRetry() {
131-
template.registerListener(new RetryListenerSupport() {
166+
template.registerListener(new RetryListener() {
132167
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
133168
Throwable t) {
134169
list.add("" + count);

0 commit comments

Comments
 (0)