1
1
/*
2
- * Copyright 2006-2022 the original author or authors.
2
+ * Copyright 2006-2024 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.
16
16
17
17
package org .springframework .retry .interceptor ;
18
18
19
- import java .util .Arrays ;
20
-
21
19
import org .aopalliance .intercept .MethodInterceptor ;
22
20
import org .aopalliance .intercept .MethodInvocation ;
23
21
24
22
import org .springframework .aop .ProxyMethodInvocation ;
23
+ import org .springframework .lang .Nullable ;
25
24
import org .springframework .retry .RecoveryCallback ;
26
25
import org .springframework .retry .RetryCallback ;
27
26
import org .springframework .retry .RetryContext ;
35
34
/**
36
35
* A {@link MethodInterceptor} that can be used to automatically retry calls to a method
37
36
* on a service if it fails. The injected {@link RetryOperations} is used to control the
38
- * number of retries. By default it will retry a fixed number of times, according to the
37
+ * number of retries. By default, it will retry a fixed number of times, according to the
39
38
* defaults in {@link RetryTemplate}.
40
- *
39
+ * <p>
41
40
* Hint about transaction boundaries. If you want to retry a failed transaction you need
42
41
* to make sure that the transaction boundary is inside the retry, otherwise the
43
42
* successful attempt will roll back with the whole transaction. If the method being
47
46
*
48
47
* @author Rob Harrop
49
48
* @author Dave Syer
49
+ * @author Artem Bilan
50
50
*/
51
51
public class RetryOperationsInterceptor implements MethodInterceptor {
52
52
53
53
private RetryOperations retryOperations = new RetryTemplate ();
54
54
55
+ @ Nullable
55
56
private MethodInvocationRecoverer <?> recoverer ;
56
57
57
58
private String label ;
@@ -71,18 +72,7 @@ public void setRecoverer(MethodInvocationRecoverer<?> recoverer) {
71
72
72
73
@ Override
73
74
public Object invoke (final MethodInvocation invocation ) throws Throwable {
74
-
75
- String name ;
76
- if (StringUtils .hasText (this .label )) {
77
- name = this .label ;
78
- }
79
- else {
80
- name = invocation .getMethod ().toGenericString ();
81
- }
82
- final String label = name ;
83
-
84
- RetryCallback <Object , Throwable > retryCallback = new MethodInvocationRetryCallback <Object , Throwable >(
85
- invocation , label ) {
75
+ RetryCallback <Object , Throwable > retryCallback = new MethodInvocationRetryCallback <>(invocation , this .label ) {
86
76
87
77
@ Override
88
78
public Object doWithRetry (RetryContext context ) throws Exception {
@@ -117,42 +107,21 @@ public Object doWithRetry(RetryContext context) throws Exception {
117
107
118
108
};
119
109
120
- if (this .recoverer != null ) {
121
- ItemRecovererCallback recoveryCallback = new ItemRecovererCallback (invocation .getArguments (),
122
- this .recoverer );
123
- try {
124
- Object recovered = this .retryOperations .execute (retryCallback , recoveryCallback );
125
- return recovered ;
126
- }
127
- finally {
128
- RetryContext context = RetrySynchronizationManager .getContext ();
129
- if (context != null ) {
130
- context .removeAttribute ("__proxy__" );
131
- }
110
+ RecoveryCallback <Object > recoveryCallback = (this .recoverer != null )
111
+ ? new ItemRecovererCallback (invocation .getArguments (), this .recoverer ) : null ;
112
+ try {
113
+ return this .retryOperations .execute (retryCallback , recoveryCallback );
114
+ }
115
+ finally {
116
+ RetryContext context = RetrySynchronizationManager .getContext ();
117
+ if (context != null ) {
118
+ context .removeAttribute ("__proxy__" );
132
119
}
133
120
}
134
-
135
- return this .retryOperations .execute (retryCallback );
136
-
137
121
}
138
122
139
- /**
140
- * @author Dave Syer
141
- *
142
- */
143
- private static final class ItemRecovererCallback implements RecoveryCallback <Object > {
144
-
145
- private final Object [] args ;
146
-
147
- private final MethodInvocationRecoverer <?> recoverer ;
148
-
149
- /**
150
- * @param args the item that failed.
151
- */
152
- private ItemRecovererCallback (Object [] args , MethodInvocationRecoverer <?> recoverer ) {
153
- this .args = Arrays .asList (args ).toArray ();
154
- this .recoverer = recoverer ;
155
- }
123
+ private record ItemRecovererCallback (Object [] args ,
124
+ MethodInvocationRecoverer <?> recoverer ) implements RecoveryCallback <Object > {
156
125
157
126
@ Override
158
127
public Object recover (RetryContext context ) {
0 commit comments