32
32
import org .springframework .data .redis .TooManyClusterRedirectionsException ;
33
33
import org .springframework .data .redis .connection .util .ByteArraySet ;
34
34
import org .springframework .data .redis .connection .util .ByteArrayWrapper ;
35
- import org .springframework .lang .NonNull ;
36
35
import org .springframework .lang .Nullable ;
37
- import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
38
36
import org .springframework .util .Assert ;
39
37
import org .springframework .util .CollectionUtils ;
40
38
import org .springframework .util .ObjectUtils ;
41
39
42
40
/**
43
41
* {@link ClusterCommandExecutor} takes care of running commands across the known cluster nodes. By providing an
44
- * {@link AsyncTaskExecutor} the execution behavior can be influenced .
42
+ * {@link AsyncTaskExecutor} the execution behavior can be configured .
45
43
*
46
44
* @author Christoph Strobl
47
45
* @author Mark Paluch
48
46
* @since 1.7
49
47
*/
50
48
public class ClusterCommandExecutor implements DisposableBean {
51
49
52
- protected static final AsyncTaskExecutor DEFAULT_TASK_EXECUTOR = new SimpleAsyncTaskExecutor ();
53
-
54
50
private int maxRedirects = 5 ;
55
51
56
52
private final AsyncTaskExecutor executor ;
@@ -71,14 +67,14 @@ public class ClusterCommandExecutor implements DisposableBean {
71
67
public ClusterCommandExecutor (ClusterTopologyProvider topologyProvider , ClusterNodeResourceProvider resourceProvider ,
72
68
ExceptionTranslationStrategy exceptionTranslation ) {
73
69
74
- this (topologyProvider , resourceProvider , exceptionTranslation , DEFAULT_TASK_EXECUTOR );
70
+ this (topologyProvider , resourceProvider , exceptionTranslation , new SimpleAsyncTaskExecutor () );
75
71
}
76
72
77
73
/**
78
74
* @param topologyProvider must not be {@literal null}.
79
75
* @param resourceProvider must not be {@literal null}.
80
76
* @param exceptionTranslation must not be {@literal null}.
81
- * @param executor can be {@literal null}. Defaulted to {@link ThreadPoolTaskExecutor }.
77
+ * @param executor the task executor to null, defaults to {@link SimpleAsyncTaskExecutor} if {@literal null }.
82
78
*/
83
79
public ClusterCommandExecutor (ClusterTopologyProvider topologyProvider , ClusterNodeResourceProvider resourceProvider ,
84
80
ExceptionTranslationStrategy exceptionTranslation , @ Nullable AsyncTaskExecutor executor ) {
@@ -90,11 +86,7 @@ public ClusterCommandExecutor(ClusterTopologyProvider topologyProvider, ClusterN
90
86
this .topologyProvider = topologyProvider ;
91
87
this .resourceProvider = resourceProvider ;
92
88
this .exceptionTranslationStrategy = exceptionTranslation ;
93
- this .executor = resolveTaskExecutor (executor );
94
- }
95
-
96
- private @ NonNull AsyncTaskExecutor resolveTaskExecutor (@ Nullable AsyncTaskExecutor taskExecutor ) {
97
- return taskExecutor != null ? taskExecutor : DEFAULT_TASK_EXECUTOR ;
89
+ this .executor = executor != null ? executor : new SimpleAsyncTaskExecutor ();
98
90
}
99
91
100
92
/**
@@ -149,9 +141,8 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
149
141
RuntimeException translatedException = convertToDataAccessException (cause );
150
142
151
143
if (translatedException instanceof ClusterRedirectException clusterRedirectException ) {
152
- return executeCommandOnSingleNode (cmd , topologyProvider .getTopology ()
153
- .lookup (clusterRedirectException .getTargetHost (), clusterRedirectException .getTargetPort ()),
154
- redirectCount + 1 );
144
+ return executeCommandOnSingleNode (cmd , topologyProvider .getTopology ().lookup (
145
+ clusterRedirectException .getTargetHost (), clusterRedirectException .getTargetPort ()), redirectCount + 1 );
155
146
} else {
156
147
throw translatedException != null ? translatedException : cause ;
157
148
}
@@ -182,7 +173,7 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
182
173
* @param cmd must not be {@literal null}.
183
174
* @return never {@literal null}.
184
175
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
185
- * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
176
+ * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
186
177
*/
187
178
public <S , T > MultiNodeResult <T > executeCommandOnAllNodes (final ClusterCommandCallback <S , T > cmd ) {
188
179
return executeCommandAsyncOnNodes (cmd , getClusterTopology ().getActiveMasterNodes ());
@@ -193,7 +184,7 @@ public <S, T> MultiNodeResult<T> executeCommandOnAllNodes(final ClusterCommandCa
193
184
* @param nodes must not be {@literal null}.
194
185
* @return never {@literal null}.
195
186
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
196
- * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
187
+ * {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
197
188
* @throws IllegalArgumentException in case the node could not be resolved to a topology-known node
198
189
*/
199
190
public <S , T > MultiNodeResult <T > executeCommandAsyncOnNodes (ClusterCommandCallback <S , T > callback ,
@@ -295,7 +286,7 @@ private <T> MultiNodeResult<T> collectResults(Map<NodeExecution, Future<NodeResu
295
286
* @param commandCallback must not be {@literal null}.
296
287
* @return never {@literal null}.
297
288
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
298
- * {@link MultiKeyClusterCommandCallback command}.
289
+ * {@link MultiKeyClusterCommandCallback command}.
299
290
*/
300
291
public <S , T > MultiNodeResult <T > executeMultiKeyCommand (MultiKeyClusterCommandCallback <S , T > commandCallback ,
301
292
Iterable <byte []> keys ) {
@@ -315,8 +306,8 @@ public <S, T> MultiNodeResult<T> executeMultiKeyCommand(MultiKeyClusterCommandCa
315
306
316
307
if (entry .getKey ().isMaster ()) {
317
308
for (PositionalKey key : entry .getValue ()) {
318
- futures .put (new NodeExecution (entry .getKey (), key ), this .executor . submit (() ->
319
- executeMultiKeyCommandOnSingleNode (commandCallback , entry .getKey (), key .getBytes ())));
309
+ futures .put (new NodeExecution (entry .getKey (), key ), this .executor
310
+ . submit (() -> executeMultiKeyCommandOnSingleNode (commandCallback , entry .getKey (), key .getBytes ())));
320
311
}
321
312
}
322
313
}
@@ -367,10 +358,6 @@ public void setMaxRedirects(int maxRedirects) {
367
358
@ Override
368
359
public void destroy () throws Exception {
369
360
370
- if (this .executor instanceof DisposableBean disposableBean ) {
371
- disposableBean .destroy ();
372
- }
373
-
374
361
if (this .resourceProvider instanceof DisposableBean disposableBean ) {
375
362
disposableBean .destroy ();
376
363
}
0 commit comments