1
1
/*
2
- * Copyright 2014-2017 the original author or authors.
2
+ * Copyright 2014-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
63
63
*
64
64
* @author James Carr
65
65
* @author Gary Russell
66
+ * @author Artem Bilan
67
+ *
66
68
* @since 1.3
67
69
*
68
70
*/
69
- public abstract class RetryInterceptorBuilder <T extends MethodInterceptor > {
71
+ public abstract class RetryInterceptorBuilder <B extends RetryInterceptorBuilder < B , T >, T extends MethodInterceptor > {
70
72
71
73
private RetryOperations retryOperations ;
72
74
@@ -100,16 +102,21 @@ public static StatelessRetryInterceptorBuilder stateless() {
100
102
return new StatelessRetryInterceptorBuilder ();
101
103
}
102
104
105
+ @ SuppressWarnings ("unchecked" )
106
+ protected final B _this () {
107
+ return (B ) this ;
108
+ }
109
+
103
110
/**
104
111
* Apply the retry operations - once this is set, other properties can no longer be set; can't
105
112
* be set if other properties have been applied.
106
113
* @param retryOperations The retry operations.
107
114
* @return this.
108
115
*/
109
- public RetryInterceptorBuilder < T > retryOperations (RetryOperations retryOperations ) {
116
+ public B retryOperations (RetryOperations retryOperations ) {
110
117
Assert .isTrue (!this .templateAltered , "Cannot set retryOperations when the default has been modified" );
111
118
this .retryOperations = retryOperations ;
112
- return this ;
119
+ return _this () ;
113
120
}
114
121
115
122
/**
@@ -118,13 +125,13 @@ public RetryInterceptorBuilder<T> retryOperations(RetryOperations retryOperation
118
125
* @param maxAttempts the max attempts.
119
126
* @return this.
120
127
*/
121
- public RetryInterceptorBuilder < T > maxAttempts (int maxAttempts ) {
128
+ public B maxAttempts (int maxAttempts ) {
122
129
Assert .isNull (this .retryOperations , "cannot alter the retry policy when a custom retryOperations has been set" );
123
130
Assert .isTrue (!this .retryPolicySet , "cannot alter the retry policy when a custom retryPolicy has been set" );
124
131
this .simpleRetryPolicy .setMaxAttempts (maxAttempts );
125
132
this .retryTemplate .setRetryPolicy (this .simpleRetryPolicy );
126
133
this .templateAltered = true ;
127
- return this ;
134
+ return _this () ;
128
135
}
129
136
130
137
/**
@@ -134,7 +141,7 @@ public RetryInterceptorBuilder<T> maxAttempts(int maxAttempts) {
134
141
* @param maxInterval The max interval.
135
142
* @return this.
136
143
*/
137
- public RetryInterceptorBuilder < T > backOffOptions (long initialInterval , double multiplier , long maxInterval ) {
144
+ public B backOffOptions (long initialInterval , double multiplier , long maxInterval ) {
138
145
Assert .isNull (this .retryOperations , "cannot set the back off policy when a custom retryOperations has been set" );
139
146
Assert .isTrue (!this .backOffPolicySet , "cannot set the back off options when a back off policy has been set" );
140
147
ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy ();
@@ -144,7 +151,7 @@ public RetryInterceptorBuilder<T> backOffOptions(long initialInterval, double mu
144
151
this .retryTemplate .setBackOffPolicy (policy );
145
152
this .backOffOptionsSet = true ;
146
153
this .templateAltered = true ;
147
- return this ;
154
+ return _this () ;
148
155
}
149
156
150
157
/**
@@ -153,37 +160,37 @@ public RetryInterceptorBuilder<T> backOffOptions(long initialInterval, double mu
153
160
* @param policy The policy.
154
161
* @return this.
155
162
*/
156
- public RetryInterceptorBuilder < T > retryPolicy (RetryPolicy policy ) {
163
+ public B retryPolicy (RetryPolicy policy ) {
157
164
Assert .isNull (this .retryOperations , "cannot set the retry policy when a custom retryOperations has been set" );
158
165
Assert .isTrue (!this .templateAltered , "cannot set the retry policy if max attempts or back off policy or options changed" );
159
166
this .retryTemplate .setRetryPolicy (policy );
160
167
this .retryPolicySet = true ;
161
168
this .templateAltered = true ;
162
- return this ;
169
+ return _this () ;
163
170
}
164
171
165
172
/**
166
173
* Apply the back off policy. Cannot be used if a custom retry operations, or back off policy has been applied.
167
174
* @param policy The policy.
168
175
* @return this.
169
176
*/
170
- public RetryInterceptorBuilder < T > backOffPolicy (BackOffPolicy policy ) {
177
+ public B backOffPolicy (BackOffPolicy policy ) {
171
178
Assert .isNull (this .retryOperations , "cannot set the back off policy when a custom retryOperations has been set" );
172
179
Assert .isTrue (!this .backOffOptionsSet , "cannot set the back off policy when the back off policy options have been set" );
173
180
this .retryTemplate .setBackOffPolicy (policy );
174
181
this .templateAltered = true ;
175
182
this .backOffPolicySet = true ;
176
- return this ;
183
+ return _this () ;
177
184
}
178
185
179
186
/**
180
187
* Apply a Message recoverer - default is to log and discard after retry is exhausted.
181
188
* @param recoverer The recoverer.
182
189
* @return this.
183
190
*/
184
- public RetryInterceptorBuilder < T > recoverer (MessageRecoverer recoverer ) {
191
+ public B recoverer (MessageRecoverer recoverer ) {
185
192
this .messageRecoverer = recoverer ;
186
- return this ;
193
+ return _this () ;
187
194
}
188
195
189
196
protected void applyCommonSettings (AbstractRetryOperationsInterceptorFactoryBean factoryBean ) {
@@ -204,7 +211,8 @@ protected void applyCommonSettings(AbstractRetryOperationsInterceptorFactoryBean
204
211
/**
205
212
* Builder for a stateful interceptor.
206
213
*/
207
- public static final class StatefulRetryInterceptorBuilder extends RetryInterceptorBuilder <StatefulRetryOperationsInterceptor > {
214
+ public static final class StatefulRetryInterceptorBuilder
215
+ extends RetryInterceptorBuilder <StatefulRetryInterceptorBuilder , StatefulRetryOperationsInterceptor > {
208
216
209
217
private final StatefulRetryOperationsInterceptorFactoryBean factoryBean =
210
218
new StatefulRetryOperationsInterceptorFactoryBean ();
@@ -238,44 +246,6 @@ public StatefulRetryInterceptorBuilder newMessageIdentifier(NewMessageIdentifier
238
246
return this ;
239
247
}
240
248
241
- @ Override
242
- public StatefulRetryInterceptorBuilder retryOperations (
243
- RetryOperations retryOperations ) {
244
- super .retryOperations (retryOperations );
245
- return this ;
246
- }
247
-
248
- @ Override
249
- public StatefulRetryInterceptorBuilder maxAttempts (int maxAttempts ) {
250
- super .maxAttempts (maxAttempts );
251
- return this ;
252
- }
253
-
254
- @ Override
255
- public StatefulRetryInterceptorBuilder backOffOptions (long initialInterval ,
256
- double multiplier , long maxInterval ) {
257
- super .backOffOptions (initialInterval , multiplier , maxInterval );
258
- return this ;
259
- }
260
-
261
- @ Override
262
- public StatefulRetryInterceptorBuilder retryPolicy (RetryPolicy policy ) {
263
- super .retryPolicy (policy );
264
- return this ;
265
- }
266
-
267
- @ Override
268
- public StatefulRetryInterceptorBuilder backOffPolicy (BackOffPolicy policy ) {
269
- super .backOffPolicy (policy );
270
- return this ;
271
- }
272
-
273
- @ Override
274
- public StatefulRetryInterceptorBuilder recoverer (MessageRecoverer recoverer ) {
275
- super .recoverer (recoverer );
276
- return this ;
277
- }
278
-
279
249
@ Override
280
250
public StatefulRetryOperationsInterceptor build () {
281
251
this .applyCommonSettings (this .factoryBean );
@@ -295,7 +265,7 @@ public StatefulRetryOperationsInterceptor build() {
295
265
* Builder for a stateless interceptor.
296
266
*/
297
267
public static final class StatelessRetryInterceptorBuilder
298
- extends RetryInterceptorBuilder <RetryOperationsInterceptor > {
268
+ extends RetryInterceptorBuilder <StatelessRetryInterceptorBuilder , RetryOperationsInterceptor > {
299
269
300
270
private final StatelessRetryOperationsInterceptorFactoryBean factoryBean =
301
271
new StatelessRetryOperationsInterceptorFactoryBean ();
0 commit comments